-1

How could I disable/hide/remove the Network Drive from the SaveFileDialog() in C#? Here is the demo code for reference.

 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.......
    }   
 }

enter image description here

  • What happens if your user wants to save the file on a network share? – Mark Benningfield Dec 24 '21 at 11:07
  • Just for the sake of security, I don't want users to save in the Network path. – Abhishek Kumar Singh Dec 24 '21 at 11:10
  • You can install Microsoft's Microsoft.WindowsAPICodePack-Shell NuGet package and use the `CommonSaveFileDialog`. The dialogs expose a `FolderChanging` event. You can handle it to check if the opening folder is a Network folder and cancel opening using the event args. – BionicCode Dec 24 '21 at 12:15
  • Otherwise you can always show an error message to force the user to pick a different destination. You should programmatically re-open the dialog for the user after the message. – BionicCode Dec 24 '21 at 12:18
  • @BionicCode But how can I hide the Network icon from the Dialog(left bottom side). – Abhishek Kumar Singh Dec 25 '21 at 06:59
  • You can't. This is not a framework level dialog. It's the OS dialog hosted in a framework container to allow some degree of convenient interaction with the application. You must delegate the responsibility to choose a secure destination to the user. If your application is *required* to enforce certain security policies, you must either ask the user to verify the location after communicating the security risks or reject particular locations by displaying a corresponding dialog or create a custom dialog. A custom dialog is not that much of an effort. You can even use the original OS icons. – BionicCode Dec 25 '21 at 13:39
  • Have a look at [Customizing your FileDialog](https://learn.microsoft.com/en-us/archive/msdn-magazine/2003/march/cutting-edge-customize-your-open-file-dialog). It is fairly old, and I'm not sure if it still works. – Harald Coppoolse Dec 26 '21 at 12:38

1 Answers1

0

You can identify the user's chosen path by following method, and if the user chooses the network path, display the appropriate message for him or her:

    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;
        }
    }

Tested in:

Visual Studio 2017, .NET Framework 4.5.2, Windows Forms

Thanks

R_J
  • 52
  • 12
  • Note: if this solution helps you, please mark as answer (don't forget to vote for me). – R_J Dec 26 '21 at 11:54
  • Isn't it a bit weird, to give the operator the possibility to click on a button, but if he does, you show him that he shouldn't do that? Wouldn't it be better to at least disable the button but preferably don't show it at all? – Harald Coppoolse Dec 26 '21 at 12:36
  • Do you know a better solution? – R_J Dec 26 '21 at 14:27
  • My advice: don't show folders that the operator shouldn't select. Read [Customizing your OpenFileDialog](https://learn.microsoft.com/en-us/archive/msdn-magazine/2003/march/cutting-edge-customize-your-open-file-dialog). Works similar for SaveFileDialog – Harald Coppoolse Dec 28 '21 at 10:05