2

I'm using SavefileDialog in C#. However, my SavefileDialog can't find the desktop folder path.

This is my code:

SaveFileDialog sfd = new SaveFileDialog();
DialogResult result = sfd.ShowDialog(this);

if( result == DialogResult.OK) {
    // do something
}

Once SaveFileDialog is started, this error pops up:

Error: 'C:\Windows\system32\config\systemprofile\Desktop' refers to a location that is unavailable.

Why does the error pop up and how can I solve it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hey, welcome to SO. Please provide more details/code for anyone here to provide useful response. Along with the code, you also should mention the OS version you have. – dj079 Sep 07 '18 at 01:36
  • check out https://stackoverflow.com/a/51931123/336511 – Daniel B Sep 07 '18 at 02:30
  • What kind of app is this? A winforms app? A service? It seems like it is running as the system user which does not have a desktop directory. – D Stanley Sep 07 '18 at 03:07
  • Well, have you checked to make sure that the path `C:\Windows\system32\config\systemprofile\Desktop` actually exists? Because the desktop path doesn't look like this normally. – 41686d6564 stands w. Palestine Sep 07 '18 at 04:22

1 Answers1

4

To make SaveFileDialog open on particular directory, use InitialDirectory:

 SaveFileDialog sfd = new SaveFileDialog();
 sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
 DialogResult result = sfd.ShowDialog(this);
 if (result == DialogResult.OK)
 {
     // do something
 }

As for your error, please provide more details or at least the whole code of the method.

KozhevnikovDmitry
  • 1,660
  • 12
  • 27