Search code examples
c#wpffolderbrowserdialog

C# WPF FolderBrowserDialog - Unable to retrieve the root folder


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


Solution

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