views:

3748

answers:

6

I'm working in Visual Studio 2005 and have added a text file that needs to be parsed by right-clicking the project in the solution explorer and add --> new item. This places the .txt file to the project folder. The debug .exe file is in the /bin/debug folder.

My main question is how to properly point to the txt file from code using relative paths that will properly resolve being two folders back, while also resolving to be in the same folder after the solution is published.

A: 

You can add a post-build event to copy the .txt file to the build output folder. Then your code can assume that the file is in the same folder as the executable.

Aidan Ryan
+2  A: 

I would add a post build step that copies the txt file into the active configuration's output folder:

xcopy "$(ProjectDir)*.txt" "$(OutDir)"

There are a few macros defined such as "$(ConfigurationName)", $(ProjectDir)

Mitch Wheat
+6  A: 

If I understand your question correctly: Select the file in the "Solution Explorer". Under properties --> Copy to Output Directory, select "Copy Always" or "Copy if Newer". For build action, select "Content". This will copy the file to the /bin/debug folder on build. You should be able to reference it from the project root from then on.

David Kreps
+2  A: 

Check out the Application Class. It has several members that can be used to locate files etc. relative to the application once it's been installed. For example Application.ExecutablePath tells you where the running exe is located; you could then use a relative path to find the file e.g. ..\..\FileToBeParsed.txt. However, this assumes that the files are deployed in the same folder structure as the project folder structure, which is not usually the case. Consider properties like CommonAppDataPath and LocalUserAppDataPath to locate application-related files once a project has been deployed.

Matt
+2  A: 

An alternative to locating the file on disk would be to include the file directly in the compiled assembly as an embedded resource. To do this, right-click on the file and choose "Embedded Resource" as the build action. You can then retrieve the file as a byte stream:

Assembly thisAssembly = Assembly.GetExecutingAssembly();
Stream stream = thisAssembly.GetManifestResourceStream("Namespace.Folder.Filename.Ext");
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);

More information about embedded resources can be found here and here.

If you're building an application for your own use, David Krep's suggestion is best: just copy the file to the output directory. If you're building an assembly that will get reused or distributed, then the embedded resource option is preferable because it will package everything in a single .dll.

Seth Petry-Johnson
A: 

Go to Project Properties -> Configuration Properties -> Debugging

Set "Working Directory" to $(TargetDir)

Then you can properly reference your file using a relative path in your code (e.g. "....\foo.xml")