you're reading...
.NET 2.0

Getting around the visual studio project directory structure

Cheez commented that he was having an issue where he had to remember to set all of his resources to copy the content or he would get File Not Found Exceptions at run time.

Once the project gets beyond a certain size this may not be desirable; especially once his project gets underway copying that content will use up a lot of disk space and possibly slow down his build to just to copy the data.

About the Resources; that is Visual C# Express’ fault. The IDE can’t make any assumptions about what you want because it leaves all of the building up to MSBuild — remember those awful makefiles from the CS classes at MTU? think the same thing but extensible for building anything. And I think they make more sense than makefile ever did.

There is one way to get around it; which is how the starter kit does it. Prepend all of your filenames with @”..\..\” to get out of the bin\Debug or bin\Release directory and to where your project is located.

Something that is interesting is that if you go into your project’s bin\debug directory you will see two executables there app.exe and app.vshost.exe. When you run your app from Visual Studio you are actually running app.vshost.exe.* You can use this knowledge to your advantage and choose at runtime what you need to prepend to the filename.

public static class FileHelper {
  private static object locker = new object();
  private static string filePrefix = null;
  public static string FilePrefix {
    get {
      if( filePrefix == null ) {
        lock( locker ) {
          if( filePrefix == null ) {
            string app = System.Environment.GetCommandLineArgs()[0];
            if( app.ToLower().EndsWith(".vshost.exe") )
              filePrefix = @"..\..\";
              filePrefix = "";
      return filePrefix;

Now when you need a filename you do: FileHelper.FilePrefix + "myfilename.ext";

Ideally you should put the filenames into a readonly static variable as well, that way if you need/want to change the filename you only have to update one location. For example:

public static class MediaFiles {
  public readonly static string TitleScreenImage = 
    FileHelper.FilePrefix + @"Images\TitleScreen.tga";

*There are caveats to this technique.

  1. You can tell Visual Studio not to use the VSHost option, in which case you need to look at the directory structure or something else. Maybe System.Diagnostics.Debugger.IsAttached, but it is possible to have the debugger attached when not running in Visual Studio as well.
  2. System.Environment.GetCommandLineArgs() doesn’t exist on the Compact Framework which the 360 version of your XNA code will run on. I would assume by the time you get to doing 360 builds of your game I can convince you to go with a customized build process so this would be a non-issue 🙂

Edit: Of course you can just change the output path of the binary as well, but at one point in time that caused severe problems for me so I stopped doing it.


About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.


One thought on “Getting around the visual studio project directory structure

  1. I was going to make a long diatribe about how this type of thing should probably be specified in app.config, and not hard-coded in the program.

    Thank goodness I IM’d you about it first, though, and you informed me that the whole System.Configuration namespace doesn’t exist in the Compact Framework / XNA.

    Posted by Jason Poll | August 31, 2006, 11:53 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: