2

I'm using

Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); 

to get the current directory I'm working in.

What I get is:

"R:\AquaSafeWDS\FrontEnds\WPF\Clients\AquaSafe.Clients.LagrangeanSimulator\bin\Debug"

but what I really want is:

"R:\AquaSafeWDS\FrontEnds\WPF\Clients\AquaSafe.Clients.LagrangeanSimulator"

Any ideas? Rui Martins

Storm
  • 684
  • 9
  • 20
Rui Martins
  • 2,164
  • 7
  • 33
  • 52
  • 4
    What you are wanting is the project directory it would seem...but how does that help you in release code? What is the goal? – DonBoitnott Jun 03 '13 at 11:18
  • @rui - please explain what are you trying to do. If you want to pass current source code path then please let us know your intention behind it. – Parag Meshram Jun 03 '13 at 11:28

4 Answers4

4

Use Directory.GetCurrentDirectory - see: http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.71).aspx

Jan Thomä
  • 13,296
  • 6
  • 55
  • 83
  • Well then this is the current working directory. After all this is where your EXE runs. The bin\Debug subfolder is how VS is laying out projects. If you need to have you EXE run in a different working directory, you may be able to instruct VS to do so. See: http://stackoverflow.com/questions/1102976/how-to-change-the-working-directory-while-debugging-in-visual-studio – Jan Thomä Jun 03 '13 at 11:19
  • Not a great solution - the working directory can change as you use the application, e.g., open file dialogs etc. – Polyfun Jun 03 '13 at 11:37
  • Well the question was how to get the current working directory. Whether or not this is a good solution to the OPs problem is hard to tell without knowing how the OP intends to use the current working directory. – Jan Thomä Jun 03 '13 at 12:18
2

If you want to access files in your project directory (where your csproj file lives), which will be in the exe directory when you distribute your application and install it on another machine, then the best solution is to add the files to your project, and in the Properties window set them to copy to the output directory. The files will automatically be copied to e.g., bin\Debug when you build your project.

Now when you use Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);, it will give you the correct directory for your files.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
1

String DirectoryName = System.IO.Directory.GetCurrentDirectory(); // It will get the name of the working directory

Taj
  • 1,718
  • 1
  • 12
  • 16
1

This is not a practical problem.

But at application start if you want some directory to be your default working directory then in project properties you could select custom working directory -

enter image description here

Parag Meshram
  • 8,281
  • 10
  • 52
  • 88