2014-09-01 198 views
5

我想列出来自网络服务器的所有共享目录。列出网络位置中的所有共享文件夹

要列出从共享网络目录的目录我用

Directory.GetDirectories(@"\\server\share\") 

的问题是我想列出的\\server所有文件夹。

如果我用同样的方法我得到一个异常说

的UNC路径应该是这样的形式\服务器\共享

我看了所有的地方的,我不能找到解决方案。

有没有人知道我应该怎么做才能显示\\share中的文件夹?

+0

[获取本地网络服务器上所有UNC共享文件夹的列表]的可能重复(http://stackoverflow.com/questions/3567063/get-a-list-of-all-unc-shared-folders-在本地网络服务器上) – kamaradclimber 2014-11-02 08:45:26

回答

1

我能找到的最好的解决办法是所谓的“网”的应用程序从一个隐藏的cmd.exe实例:

public static string[] GetDirectoriesInNetworkLocation(string networkLocationRootPath) 
{ 
    Process cmd = new Process(); 
    cmd.StartInfo.FileName = "cmd.exe"; 
    cmd.StartInfo.RedirectStandardInput = true; 
    cmd.StartInfo.RedirectStandardOutput = true; 
    cmd.StartInfo.CreateNoWindow = true; 
    cmd.StartInfo.UseShellExecute = false; 
    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    cmd.Start(); 
    cmd.StandardInput.WriteLine($"net view {networkLocationRootPath}"); 
    cmd.StandardInput.Flush(); 
    cmd.StandardInput.Close(); 

    string output = cmd.StandardOutput.ReadToEnd(); 

    cmd.WaitForExit(); 
    cmd.Close(); 

    output = output.Substring(output.LastIndexOf('-') + 2); 
    output = output.Substring(0, output.IndexOf("The command completed successfully.")); 

    return 
     output 
      .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) 
      .Select(x => System.IO.Path.Combine(networkLocationRootPath, x.Substring(0, x.IndexOf(' ')))) 
      .ToArray(); 
} 

根据你的使用情况,您可能需要验证networkLocationRootPath,以避免任何cmd注射问题。

0

根据codeproject.com,这似乎是.net的缺失部分。该网站仍然描述了在2003年工作的solution.

你可以试试看,并解释它是否有效?

+0

这不是OP想要的。这是列出从您的机器共享的文件夹。 OP(和I)希望列出网络中另一台计算机的共享文件夹。 – 2017-06-15 23:58:11

+1

我真的很高兴从〜3年前得到一个答案的评论:) – kamaradclimber 2017-06-16 07:16:54

+0

我正在查看这个,这是提出的问题之一。我现在添加了我发现的答案。 – 2017-06-19 05:43:25

2

我知道这个线程是旧的,但这个解决方案可能最终会帮助某人。我使用了一个命令行,然后从包含目录名称的输出中返回一个子字符串。

static void Main(string[] args) 
    { 
     string servername = "my_test_server"; 
     List<string> dirlist = getDirectories(servername); 
     foreach (var dir in dirlist) 
     { 
      Console.WriteLine(dir.ToString()); 
     }  
     Console.ReadLine(); 
    } 

    public static List<string> getDirectories (string servername) 
    { 
     Process cmd = new Process(); 
     cmd.StartInfo.FileName = "cmd.exe"; 
     cmd.StartInfo.RedirectStandardInput = true; 
     cmd.StartInfo.RedirectStandardOutput = true; 
     cmd.StartInfo.CreateNoWindow = true; 
     cmd.StartInfo.UseShellExecute = false; 
     cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     cmd.Start(); 
     cmd.StandardInput.WriteLine("net view \\\\" + servername); 
     cmd.StandardInput.Flush(); 
     cmd.StandardInput.Close(); 
     string output = cmd.StandardOutput.ReadToEnd(); 
     cmd.WaitForExit(); 
     cmd.Close(); 
     List<string> dirlist = new List<string>(); 
     if(output.Contains("Disk")) 
     { 
      int firstindex = output.LastIndexOf("-") + 1; 
      int lastindex = output.LastIndexOf("Disk"); 
      string substring = ((output.Substring(firstindex, lastindex - firstindex)).Replace("Disk", string.Empty).Trim()); 
      dirlist = substring.Split('\n').ToList(); 
     } 
     return dirlist; 
    } 
相关问题