1

Please see the code below:

string test = System.IO.Directory.GetCurrentDirectory();

Up to today; test would return the directory of my project i.e. C:\MySolution

The MySolution folder contains MySolution.sln.

Now it returns: "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE"

What is the problem? I have spent time Googling this and I have also looked here: https://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.110).aspx

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • Where you run your exe? or Run from Visual Studio? The executable file of Visual Stidio process devenv.exe is in "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE" folder – H.Yang Aug 21 '17 at 09:38
  • @Ho yang, I run the executeable from Visual Studio. – w0051977 Aug 21 '17 at 09:38
  • That value can change. Why are you relying on it? Why does it matter that it is pointing to a different folder? What are you using `test` for? – mjwills Aug 21 '17 at 09:39
  • Right click the project properties, on the debug tab, enter a value in 'Working directory'. This is stored in the .csproj.user file, so may have changed if this isn't checked into source control and you have re-checked out to a new working directory. Other reasons for it changing could be whether you are running in debug or release mode, or whether you are running VS as administrator or not. – BenTaylor Aug 21 '17 at 09:41
  • I am using it to specify the path of: DataDirectory (https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc31ea59-5718-49b6-9f1f-7039da425296/where-is-datadirectory-?forum=sqlce) – w0051977 Aug 21 '17 at 09:41
  • ref: https://stackoverflow.com/questions/4815423/how-do-i-set-the-working-directory-to-the-solution-directory-in-c – H.Yang Aug 21 '17 at 09:43
  • and ref: https://stackoverflow.com/questions/1367732/finding-current-directory-during-visual-studio-debugging-session – H.Yang Aug 21 '17 at 09:43
  • If you always want to use the path to the directory where your executable is located, then consider this instead: `Path.GetDirectoryName(Assembly.GetEntryAssembly().CodeBase)` – Rufus L Aug 21 '17 at 09:47
  • Do you really need current directory? This only make sense if your software is intended to have different working folder, e.g. when it's changed by shortcut to exe file or when it's a console application which can be run from another folder. I think you simply want an [exe-folder](https://stackoverflow.com/q/3991933/1997232). – Sinatr Aug 21 '17 at 09:50

3 Answers3

0

It's the code which I'm using (so far reliable):

new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)).LocalPath
Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
0

what project is this, is it a console/winform/web ?

If you are using winform

private void PrintStartupPath() {
    textBox1.Text = "The path for the executable file that " +
       "started the application is: " +
       Application.StartupPath;
 }

If you are using console

static void Main(string[] args)
{
       string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
       Console.WriteLine(appDirectory);
}

if you are using web

protected void Application_Start(object sender, EventArgs e)
 {
     string path2 = Server.MapPath("~");
 }
Feli Wen
  • 41
  • 3
0

I am having this problem with nearly every method that is an option to get the project directory.

-GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase

-AppDomain.CurrentDomain.BaseDirectory

-Environment.CurrentDirectory

several others listed here: Best way to get application folder path

All return Visual Studio Program Files path you mentioned above. It must be a bug with the new visual studio, as I just upgraded.

Amruth
  • 5,792
  • 2
  • 28
  • 41
cpmcgee
  • 1
  • 1