列出网络位置上的所有共享文件夹

7

我希望能够列出网络服务器所有共享目录。

为了列出共享网络目录中的目录,我使用了

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

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

如果我使用相同的方法,我会得到一个异常,显示:

UNC 路径应该是 \server\share 的形式

我找了很多地方,但是我找不到解决方法。

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


可能是重复的问题:在本地网络服务器上获取所有UNC共享文件夹的列表 - kamaradclimber
4个回答

3

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

    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
我找到的最佳解决方案是从隐藏的cmd.exe实例中调用“net”应用程序:
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注入问题。


1
我无法编辑或评论Sellorio的答案,所以我写了一个新答案:
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);

0
这似乎是一个缺失的部分,根据codeproject.com的说法它是.net的一部分。然而,该网站描述了一个在2003年起作用的solution
你能试试看并解释它是否有效吗?

1
这不是OP想要的。这是列出从您的计算机共享的文件夹。OP(和我)想要列出网络上另一台计算机共享的文件夹。 - Sellorio
2
我真的很高兴收到了大约3年前回答的评论 :) - kamaradclimber
我在查找相关信息时发现了这个问题。现在,我已经将我找到的解决方案添加为答案。 - Sellorio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接