List all shared folders from a network location

I want to list all shared directories from a network server.

To list directories from a shared network directory, I used

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

The problem is that I want to list all the folders on the \\server .

If I use the same method, I get an exception saying

The UNC path must be of the form \ server \ share

I have looked everywhere and I cannot find a solution for this.

Does anyone have an idea of ​​what I should do to display folders in \\share ?

+5
source share
3 answers

The best solution I could find was to call the "net" application from a hidden instance of 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(); } 

Depending on your use case, you might want to check networkLocationRootPath to avoid cmd issue issues.

+1
source

I know this thread is outdated, but this solution may ultimately help someone. I used the command line, then returned a substring from its output containing the directory names.

  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; } 
+2
source

This seems to be the missing part of .net according to codeproject.com. The website, however, describes a solution that worked in 2003.

Can you try and explain if it works?

0
source

Source: https://habr.com/ru/post/1201575/


All Articles