0

I need to access working directory from code in C# in MSVS 2013 like:

C:\SomePath\Visual\NameOfTheProject\

In java we can access it System.getProperty("user.dir")

Does C# have equivalent?

@update

Problem is that I need to search folder X for implementations of some interface. App will be executed on different machines, so I should have a path which will always navigate me to folder X

@update2

I need to look for the folder with dlls which is named X in the folder where I execute the app (so my assembly).

svick
  • 236,525
  • 50
  • 385
  • 514
krzakov
  • 3,871
  • 11
  • 37
  • 52
  • Do either of: http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx http://social.msdn.microsoft.com/Forums/en-US/7d8798db-32eb-4886-9531-31b3decba018/how-can-i-get-the-current-working-directory-at-runtime?forum=csharplanguage help? – darthbith May 04 '14 at 19:52
  • 1
    Do you want the current directory of your executable? – Hamlet Hakobyan May 04 '14 at 19:55
  • Well thing is I need to search my directory for implementation of some interface in dll's file. I assume current directory will be nice. – krzakov May 04 '14 at 19:56
  • 1
    Regarding the update: What is folder `X`? Is it the folder where the application is installed? Or the directory from where it was invoked? – Anders Abel May 04 '14 at 20:04
  • What @AndersAbel is asking, do you mean the folder where the application is (The actual .exe), or where it is opened from (Ex: A shortcut on the desktop) – Cyral May 04 '14 at 20:05
  • @AndersAbel I'm on a project stage. Project path `C:/Project/` and it contains folders `X` so `(C:/Project/X)` and inside there is some `dll's` which I need to analyze. @Cyral actual.exe file – krzakov May 04 '14 at 20:07
  • @krzakov I've updated my answer with a method to get where the exe is found. – Cyral May 04 '14 at 20:16

2 Answers2

2

Directory.GetCurrentDirectory() will return the current working directory of the process. That is the equivalent of the "user.dir" property in Java.

To get the directory of the exe (or actually, the entry assembly, which is normally the exe), use Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • While this answer is correct in itself, I am not convinced that is what the OP is looking for, at least not any more after the explanatory comment of what this is for. – O. R. Mapper May 04 '14 at 19:59
  • Checking. I need path, where I execute the app . – krzakov May 04 '14 at 20:01
  • You're right. I looked up the `"user.dir"` property to be sure I presented the exact equivalent, but it might not be what OP wants. – Anders Abel May 04 '14 at 20:02
1

To get the absolute path to the executable file (And not where it was executed from, if it was through a shortcut) you can use:

System.IO.Path.GetDirectoryName(Application.ExecutablePath); (Needs to be using Windows Forms)

or

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

Both will return the directory that your executable is found in.

To get the path of another folder within your executable's directory, you can use this:

Path.Combine(path, "X");

Where path is any of the above examples.

Cyral
  • 13,999
  • 6
  • 50
  • 90