【问题标题】:Disable the Network Drive from the SaveFileDialog() in C#从 C# 中的 SaveFileDialog() 禁用网络驱动器
【发布时间】:2021-12-24 10:52:21
【问题描述】:

如何从 C# 中的 SaveFileDialog() 禁用/隐藏/删除网络驱动器? 这是演示代码供参考。

 using (var browsDlg = new SaveFileDialog())
 {
    browsDlg.Title = "Save File";
    // What should be written here to hide the Network Path
    
    if (browsDlg.ShowDialog() == DialogResult.OK)
    {
       // Something.......
    }   
 }

【问题讨论】:

  • 如果您的用户想要将文件保存在网络共享上会发生什么?
  • 为了安全起见,我不希望用户保存在网络路径中。
  • 您可以安装 Microsoft 的 Microsoft.WindowsAPICodePack-Shell NuGet 包并使用CommonSaveFileDialog。这些对话框公开了一个FolderChanging 事件。您可以处理它以检查打开的文件夹是否为网络文件夹并使用事件参数取消打开。
  • 否则,您始终可以显示错误消息以强制用户选择不同的目的地。您应该在消息后以编程方式为用户重新打开对话框。
  • @BionicCode 但是如何从对话框中隐藏网络图标(左下角)。

标签: c# .net wpf winforms savefiledialog


【解决方案1】:

您可以通过以下方法识别用户选择的路径,如果用户选择了网络路径,则为他或她显示相应的消息:

    string FileSavePath;
    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog FBD = new FolderBrowserDialog();
        switch (FBD.ShowDialog())
        {
            case DialogResult.OK:
                FileSavePath = FBD.SelectedPath.Substring(0,2);
                switch (FileSavePath == @"\\")
                {
                    case true:
                        MessageBox.Show(null, "You are not allowed to use the network path", "Warning", MessageBoxButtons.OK);
                        FileSavePath = null;
                        break;
                }
                break;
        }
    }

测试于:

Visual Studio 2017, .NET 框架 4.5.2, Windows 窗体

谢谢

【讨论】:

  • 注意:如果这个解决方案对你有帮助,请标记为答案(不要忘记投票给我)。
  • 给操作员点击按钮的可能性是不是有点奇怪,但如果他这样做了,你告诉他他不应该这样做吗?至少禁用按钮但最好不显示它不是更好吗?
  • 你知道更好的解决方案吗?
  • 我的建议:不要显示操作员不应该选择的文件夹。阅读Customizing your OpenFileDialog。 SaveFileDialog 的工作原理类似
猜你喜欢
  • 1970-01-01
  • 2017-07-19
  • 2010-11-28
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
相关资源
最近更新 更多