c# - C# winforms 上的文件夹浏览器对话框

标签 c# .net winforms folderbrowserdialog

我在 winform 中使用 folderBrowserDialog。

我需要默认或初始路径作为网络位置。

例如:

folderBrowserDialog1.SelectedPath = @"\\server1\foo\bar\";

这是行不通的。我的系统在正确的网络上,我可以通过浏览器访问目录并运行命令。

这是非特征吗?还是有解决方法? 如果有人可以指导我,我将不胜感激!

谢谢, 伊瓦尔

最佳答案

根据我的经验,.NET 在 UNC 路径方面的表现一直很不稳定。有时有效,有时无效。我确信对此有一个很好的解释,但早些时候,我搜索了又搜索,但没有找到答案。

我没有处理这个问题,而是采用了最好自己映射驱动器然后在代码中完成后断开连接的策略。 (如果你找到答案,我很想知道这是为什么,但由于我有一个可行的解决方案,我不太关心自己研究它。)它对我们 100% 的时间都有效,而且它是好简单。我为此创建了一个类,因为这是我们店里的一项常见任务。

无论如何,我不知道您是否愿意接受这个想法,但如果您有兴趣并且还没有代码,我们的例程将粘贴在下面。检查打开的驱动器盘符并映射它并在完成后断开连接将相当简单。

public static class NetworkDrives
    {
        public static bool  MapDrive(string DriveLetter, string Path, string Username, string Password)
        {

            bool ReturnValue = false;

            if(System.IO.Directory.Exists(DriveLetter + ":\\"))
            {
                DisconnectDrive(DriveLetter);
            }
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": " + Path + " " + Password + " /user:" + Username;
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }
        public static bool DisconnectDrive(string DriveLetter)
        {
            bool ReturnValue = false;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.FileName = "net.exe";
            p.StartInfo.Arguments = " use " + DriveLetter + ": /DELETE";
            p.Start();
            p.WaitForExit();

            string ErrorMessage = p.StandardError.ReadToEnd();
            string OuputMessage = p.StandardOutput.ReadToEnd();
            if (ErrorMessage.Length > 0)
            {
                throw new Exception("Error:" + ErrorMessage);
            }
            else
            {
                ReturnValue = true;
            }
            return ReturnValue;
        }

    }

关于c# - C# winforms 上的文件夹浏览器对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3428472/

相关文章:

C# 如何在 Debug模式下从另一个 clickonce winforms 项目启动 clickonce winforms 项目

c# - 根据 Windows 主题更改面板的主题(颜色)

c# - 如何从代理类中的 Web 服务响应中读取 http 响应 soap header

.net - 使用 .NET Core 进行 DAL 和连接字符串的依赖注入(inject)

c# - x64 系统上的 x86 应用程序使用哪个 ngen?

c# - 无法将电子邮件发送到带有斯堪的纳维亚字符的地址

c# - 用户定义值类型的装箱

c# - 透明绘图复合图层而不是重绘

c# - 获取该月下一个第 n 天的 DateTime

c# - 单一方法或泛型 It.IsAny 的模拟行为松散