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.
source share