0

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?

Christian Severin
  • 1,793
  • 19
  • 38
Andy Hunt
  • 1,053
  • 1
  • 11
  • 26

1 Answers1

2

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.

Sam Jenkins
  • 1,284
  • 1
  • 12
  • 30
  • Is `Application` available inside a class library? My code currently isn't recognising it! – Andy Hunt Aug 04 '11 at 12:50
  • I just checked my code further, in order to use that I had imported System.Windows.Forms, which you may not wish to do. The following link has a second suggested method to achieve same result using reflection (http://www.csharp-examples.net/get-application-directory/). – Sam Jenkins Aug 04 '11 at 12:55
  • Aye, I did the same thing. I forgot what namespace it was under. However: it didn't work. It would appear it's starting up inside the wrong directory to begin with. I haven't set the startup or working directory to that, as far as I'm aware. – Andy Hunt Aug 04 '11 at 12:57
  • So if you `Console.WriteLine()` (or something) to get the application start up path it says its in `C:\Program Files\Visual Studio 2010\Common7\IDE\`? – Sam Jenkins Aug 04 '11 at 13:01
  • Correct, for both Environment.CurrentDirectory and Application.StartupPath – Andy Hunt Aug 04 '11 at 13:03
  • Are you running the code through the immediate window by chance or do you have a test Windows form/console application, which you are running this from? If you are running it through the immediate window, then the application that is running the DLL would probably be devenv which is in the directory you've specified. – Sam Jenkins Aug 04 '11 at 13:08
  • I'm running it through a WCF service. That could be a root of the problem, but the odd thing is it worked just fine at first, then something happened (I don't know what) and now it looks in the wrong directory. – Andy Hunt Aug 04 '11 at 13:09
  • Yeah that sounds like it, WCF uses WcfSvcHost.exe as the default host, which is in that location. I can't explain why it would have worked before. My suggestion would be to either store a location in some form of configuration, where you would look for this file or to use the second method in the link above and then test for a class that is in the DLL, that way it should get the DLL location, rather than the host application. – Sam Jenkins Aug 04 '11 at 13:12
  • The sad thing is this has to be portable. The DLL reads the app.config file of its consumer to get the path (to the xml file), but the path needs to be relative as I've no way of knowing where it will end up! It's also under version control, so others editing it (or myself on another machine) would find it broken with a full path. – Andy Hunt Aug 04 '11 at 13:22
  • You could try the second suggestion "to use the second method in the link above and then test for a class that is in the DLL, that way it should get the DLL location, rather than the host application", this should then deal with it picking up the DLL location without having to store it in the code or in a configuration file/database. – Sam Jenkins Aug 04 '11 at 13:25