我希望能够列出网络服务器所有共享目录。
为了列出共享网络目录中的目录,我使用了
Directory.GetDirectories(@"\\server\share\")
问题是我想列出 \\server
上的所有文件夹。
如果我使用相同的方法,我会得到一个异常,显示:
UNC 路径应该是 \server\share 的形式
我找了很多地方,但是我找不到解决方法。
有人知道我应该怎么做才能显示 \\share
中的文件夹吗?
我希望能够列出网络服务器所有共享目录。
为了列出共享网络目录中的目录,我使用了
Directory.GetDirectories(@"\\server\share\")
问题是我想列出 \\server
上的所有文件夹。
如果我使用相同的方法,我会得到一个异常,显示:
UNC 路径应该是 \server\share 的形式
我找了很多地方,但是我找不到解决方法。
有人知道我应该怎么做才能显示 \\share
中的文件夹吗?
我知道这篇帖子很旧了,但是这个解决方案可能最终会帮助到某些人。我使用了一个命令行,然后从其输出中返回了一个包含目录名称的子字符串。
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;
}
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注入问题。
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();
}
如果您的执行路径中有“-”,则会在以下行出现零长度错误:
output = output.Substring(output.LastIndexOf('-') + 2);
首先通过删除路径修复此问题:
string CurrentExecutePath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
output = output.Replace(CurrentExecutePath, "");
output = output.Substring(output.LastIndexOf('-') + 2);