SaveFileDialog Changing Environment.CurrentDirectory

J

jehugaleahsa

Hello:

Correct me if I'm wrong, but it appears that saving a file with the
SaveFileDialog changes the Environment.CurrentDirectory property.

How can I prevent this?

Thanks,
Travis
 
J

jehugaleahsa

Hello:

Correct me if I'm wrong, but it appears that saving a file with the
SaveFileDialog changes the Environment.CurrentDirectory property.

How can I prevent this?

Thanks,
Travis

Here's an example and a band-aid:

string currentDirectory = Environment.CurrentDirectory;
try
{
string directory = "C:\\temp";
SaveFileDialog dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.CreatePrompt = false;
dialog.DefaultExt = ".csv";
dialog.FileName = "example.csv";
dialog.InitialDirectory = directory; // <-- Most
likely the culprit
dialog.OverwritePrompt = true;
dialog.SupportMultiDottedExtensions = true;
while (dialog.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("You must select a save
file");
}
return dialog.FileName;
}
}
finally
{
Environment.CurrentDirectory = currentDirectory;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top