How to use relative paths when debugging in Visual Studio 2008?

Hello everyone,

I have a project one computer in the My Documents folder. When I copy it to another user computer (under their My Documents directory) I want it to work with no tweaking.

Do Macros work? No.

My build process uses build events to copy files to an Install directory and since build events uses macros so it works perfectly. I want to use these same Macros in either or both of two debugging options:

  • Start external program
  • Working directory

So I attempt to use the same Macros:

Start external program: $(SolutionDir)\Install\Program.exe

This fails with the following error:

Working Directory:
The working directory you entered does not exist.  Please enter a valid working directory.

Same failure pretty much if I try to use a macro int he Working directory text field.

Well, that is a let down.

Do Environment Variables Work? No.

So I tried to use environment variables. They didn’t work either.

Start external program:    “%USERPROFILE%\My Documents\My Project\Install\Program.exe”

That wasn’t a good solution anyway, cause I want it to work whether copied to a Desktop or a D: drive or whatever.

What can I do?

Well, I loaded up Process Monitor for Sysinterals Suite and checked where we look. I configured it to just look for the executable.

Start external program:    Program.exe

Turns out we check for the executable relative to either of two paths:

  • In the YourProjectDir\bin\debug\Program.exe (or if you are doing a release build, YourProjectDir\bin\release\Program.exe)
  • The current working directory for Visual Studio 2008’s devenv.exe process.
    • Ok, so if Visual Studio 2008 was launched by double-clicking the the solution file, the working directory is the directory the solution file is in.
    • If you open Visual Studio using the Start | All Programs | Microsoft Visual Studio 2008 | Launch Microsoft Visual Studio 2008 shortcut, then the working directory is “C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\” (or the equivalent 32 bit path).

Ok, so it will check relative to the Project directory without putting in a macro if I open Visual Studio by double-clicking the the solution file. I can work with that.

So if I have an Install directory that contains the Program.exe and if I open Visual Studio by double-clicking the the solution file, I can put this in the Start external program:

Start external program:    Install\Program.exe

I left the Working directory blank.

It worked!

To bad if I ever forget to open by double-clicking the the solution file, and I instead use the shortcut it doesn’t work.  But no biggie, I can close and re-open correctly.

What else did I try?

I thought that if I added a relative path, it would check there.  So I should be able to put something like this:

Start external program:   Program.exe
Working Directory:    Install

But the relative path doesn’t work the same way.  Anything put there is only relative to YourProjectDir\bin\debug (or if you are doing a release build, YourProjectDir\bin\release), so this didn’t work.

I tried to use environment variables, but they didn’t work either.

I tried Macros, they didn’t work either.

I tried this:

Start external program:   Program.exe
Working Directory:    ..\..\..\Install

Nope, that didn’t work.

So what is the working directory when debugging/running from Visual Studio 2008?

I loaded up a project with an Install directory and debugging set to run the executable from the install directory.  I added this line to the program and put a break point on it:

string workingDirectory = Directory.GetCurrentDirectory();

The working directory appears to remain this:

YourProjectDir\bin\debug\ (or if you are doing a release build, YourProjectDir\bin\release\)

Oh well.

In a few weeks I am going to try Visual Studio 2010 and I will have to check if they improved this.


  1. […] 參考資料: […]

  2. says:

    How to use relative paths when debugging in Visual Studio 2008? | Rhyous

  3. Benjamin says:

    Is this a typo in the article?


    I ask because $(SolutionDir), for me, already includes a trailing backslash.

  4. goombaloon says:

    Thanks for the info! You answered the question I posted on StackOverflow.

  5. rhyous says:

    Hmmm...I think you missed the intention of my post (or I didn't make the intention clear which is probably more likely)

    Are you using the "Start external program" setting in the Project Properties under Debug?
    Are you sharing the code with others?

    If you answered Yes to both, then this post is for you. From your note, I think you might have been looking at something else.

    See if I check in the code and project file, then someone else checks it out, they have to change the "Start external program" setting before debugging because their path is different.

  6. Jodo says:

    Works fine for me in VS2008. Wrote a C# program that extracts from cab files and wanted to make sure the new file was correctly replaced.

    I work with the relative directory, but what is the rest of the path? This worked for me (condensed version of code)

    string str2 = "caboutput\itcc.dll";
    new CabInfo(CABFILE_FILENAME).UnpackFile(@str1, @str2);

    string destinationLoc = Directory.GetCurrentDirectory();
    destinationLoc = destinationLoc + "\\" + str2;

    if (File.Exists(@str2))
    Console.WriteLine("Current directory:" + @destinationLoc);

    this outputs everything including: (drive letter, relative folder location) + (folder in relative + subfolders + filename)

Leave a Reply

How to post code in comments?