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

Re: SaveFileDialog Changing Environment.CurrentDirectory by jehugaleahsa

jehugaleahsa
Fri May 09 14:56:31 CDT 2008

On May 9, 1:45=A0pm, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
> 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 =3D Environment.CurrentDirectory;
try
{
string directory =3D "C:\\temp";
SaveFileDialog dialog =3D new SaveFileDialog();
dialog.AddExtension =3D true;
dialog.CreatePrompt =3D false;
dialog.DefaultExt =3D ".csv";
dialog.FileName =3D "example.csv";
dialog.InitialDirectory =3D directory; // <-- Most
likely the culprit
dialog.OverwritePrompt =3D true;
dialog.SupportMultiDottedExtensions =3D true;
while (dialog.ShowDialog() !=3D DialogResult.OK)
{
MessageBox.Show("You must select a save
file");
}
return dialog.FileName;
}
}
finally
{
Environment.CurrentDirectory =3D currentDirectory;
}

Re: SaveFileDialog Changing Environment.CurrentDirectory by Peter

Peter
Fri May 09 15:42:09 CDT 2008

On Fri, 09 May 2008 12:45:02 -0700, jehugaleahsa@gmail.com =

<jehugaleahsa@gmail.com> wrote:

> 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?

This has been answered before:
http://groups.google.com/groups/search?q=3Dgroup%3Amicrosoft.public.dotn=
et.languages.csharp+file+dialog+current+directory&qt_s=3DSearch

In some of the top threads related to that search, you'll find mention =

that the FileDialog.RestoreDirectory property controls this behavior.

Of course, I suppose you could have also just looked at the documentatio=
n =

for the SaveFileDialog. IMHO, the first thing anyone should do when the=
y =

are trying to find the answer to a question related to the use of a =

particular framework class is read through the entire list of public =

members of the class in question. Often what you're looking for will be=
=

found there. :)

If you look through the previous threads that Google shows you, you will=
=

also see good advice that generally you should not be relying on the =

current directory anyway. Sometimes when dealing with legacy code it's =
=

safer to just not change the current directory. But it's usually better=
=

to just use full path names when referencing the file system. Then you'=
re =

not dependent on side-effects of other code and/or use actions.

Pete