Andy Hunt
Andy Hunt

Reputation: 1083

Current working directory is visual studio directory

I'm building a class library which references a file in its working directory. However, it's unable to find the file because it keeps looking in C:\Program Files\Visual Studio 2010\Common7\IDE\. The solutions is not in that directory, nor is the working directory set to that. I can't see anything related to my class library in that folder either!

This happens whether I use Environment.CurrentDirectory or not, when constructing my file path.

Does anybody know why it might be doing this?

Upvotes: 0

Views: 5586

Answers (1)

Sam Jenkins
Sam Jenkins

Reputation: 1314

From the sounds of it you want to be using Application.StartupPath when constructing your file path so something along the lines of;

System.IO.Path.Combine(Application.StartupPath, "myfile.txt");

If you want to set the current directory to the right directory (although personally I would recommend using the Application.StartupPath rather than trust the current directory will be right) you can do this;

System.IO.Directory.SetCurrentDirectory(Application.StartupPath);

As for why, I've had occasions where you do something like open a file open dialog then navigate to another location, which then becomes the current directory.

Upvotes: 2

Related Questions