1

I am using the FolderBrowserDialog to select the Folder where user can save file. While opening the folder browse dialog, application get crashed and raised below exception.

   System.InvalidOperationException: Unable to retrieve the root folder.
   at System.Windows.Forms.FolderBrowserDialog.RunDialog(IntPtr hWndOwner)
   at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
   at TestApp.GenerateExcelReport()

Users are using the applications from cloud as My application is launched on Citrix Environment. Hence not all the users has facing this issue but few of them are facing.

string folderPath = "X:\\Reports";
if (!Directory.Exists(folderPath))
{
    Directory.CreateDirectory(folderPath);
}

FolderBrowserDialog folderDialog = new FolderBrowserDialog();
folderDialog.ShowNewFolderButton = true;
folderDialog.SelectedPath = folderPath;
DialogResult dialogResult = folderDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
    folderPath = folderDialog.SelectedPath;
}
else
{
    return;
}

Kindly suggest what can be the root cause of getting this issue while showing the FolderBrowseDialog. Also, suggest the potential fixes to prevent this exception.

Thanks & Regards,

Hiren Lad

Hiren Lad
  • 55
  • 5
  • 1
    I assume X is a network drive. Perhaps it isn't connected or recognized as connected? – Marcus Runge Dec 24 '20 at 08:06
  • It appears that Directory.CreateDirectory doesn't throw. That method returns a value, see what that is. Is it a network mapped drive? Try not to set the SelectedPath property. See if you have this Key and Value in the registry: `HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system` -> `"EnableLinkedConnections"=[DWORD]:00000001` – Jimi Dec 24 '20 at 08:46

2 Answers2

1

This occurs for me when the folder %USERPROFILE%\Desktop doesn't exist. Creating that folder resolved the issue. Note that File Explorer may show a Desktop folder in %USERPROFILE% even if Desktop doesn't actually exist. You can verify whether Desktop exists by running dir %USERPROFILE% in cmd.

event44
  • 630
  • 5
  • 17
0

I'm late but the fix for this issue might be if you set the RootFolder property of FolderBrowserDialog other than the default RootFolder. e.g

FolderBrowserDialog folderDialog = new FolderBrowserDialog();
folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;