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.