2014-09-01 200 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; 
    } 
相關問題