Exemplo n.º 1
0
        /// <summary>
        /// Asynchronously returns a collection of IPHostEntries of all computers found in the NetworkNeighborhood
        /// </summary>
        /// <returns></returns>
        private async Task <ICollection <IPHostEntry> > DoGetHostsAsync()
        {
            var stopWatch = Stopwatch.StartNew();

            // Enumerate all computers in the NetworkNeighborhood for all Domains / Workgroups
            var hosts = new Dictionary <String, Task <IPHostEntry> >();
            ICollection <String> hostNames;

            // The WNetEnum API requires at least to be impersonated as NetworkService; this is the fallback credential used for
            // the root path of NetworkNeighborhoodResourceProvider if no other credentials have been entered.
            using (ServiceRegistration.Get <IImpersonationService>().CheckImpersonationFor(NetworkNeighborhoodResourceProvider.RootPath))
                hostNames = NetworkResourcesEnumerator.EnumerateResources(ResourceScope.GlobalNet, ResourceType.Disk, ResourceUsage.All, ResourceDisplayType.Server);

            // The hostNames returned by the NetworkResourceEnumerator are in the form \\COMPUTERNAME
            // We have to remove the leading \\ to be able to pass the hostName to GetHostEntryAsync
            foreach (var hostName in hostNames)
            {
                hosts[hostName.Substring(2)] = Dns.GetHostEntryAsync(hostName.Substring(2));
            }

            // Wait until an IPHostEntry was found for all computers
            try
            {
                await Task.WhenAll(hosts.Values);
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Error("WNetEnumNeighborhoodBrowser: Error while getting IPHostEntries", e.Message);
            }

            // Assemble the result
            var result = new HashSet <IPHostEntry>();

            foreach (var kvp in hosts)
            {
                // If the call to GetHostEntryAsync was successful, we return the retrieved IPHostEntry
                // If not, we return a new IPHostEntry object just with its HostName property set.
                var host = kvp.Value.IsFaulted ? new IPHostEntry {
                    HostName = kvp.Key
                } : kvp.Value.Result;
                NeighborhoodBrowserHelper.ConvertDnsHostNameToNetBiosName(host);
                result.Add(host);
            }
            NeighborhoodBrowserHelper.LogResult(result, GetType().Name, stopWatch.ElapsedMilliseconds);
            return(result);
        }
 public ICollection <IFileSystemResourceAccessor> GetChildDirectories()
 {
     if (_path == "/")
     {
         return(NetworkResourcesEnumerator.EnumerateResources(ResourceScope.GlobalNet, ResourceType.Disk,
                                                              ResourceUsage.All, ResourceDisplayType.Server).Select(
                    serverName => new NetworkNeighborhoodResourceAccessor(
                        _parent, StringUtils.CheckPrefix(serverName, @"\\").Replace('\\', '/'))).Cast <IFileSystemResourceAccessor>().ToList());
     }
     if (IsServerPath(_path))
     {
         return(SharesEnumerator.EnumerateShares(StringUtils.RemovePrefixIfPresent(_path, "//")).Where(
                    share => share.ShareType == ShareType.Disk).Select(
                    share => {
             try { return new NetworkNeighborhoodResourceAccessor(_parent, share.UNCPath.Replace('\\', '/')); }
             catch (IllegalCallException) { return null; }
         }
                    ).Where(share => share != null).Cast <IFileSystemResourceAccessor>().ToList());
     }
     return(_underlayingResource == null ? null : WrapLocalFsResourceAccessors(_underlayingResource.GetChildDirectories()));
 }