views:

301

answers:

3

Hi,

I think this question might be trivial for the most of you but somehow I cannot find the answer... so, I have a Visual Studio 2008 Solution foo and a Project bar in this solution. In my bar-Project i've got a Directory called baz, it looks like this:

foo dir
 |
 +-> bar dir
      |
      +-> baz dir
           |
           +-> asd file

now I want to open my "asd file" in bar-project but need the exact path to "asd file", how can I find it? I tried simply "baz\asd" but it doesn't work.

Thanks!

J

+1  A: 

If you mean open at runtime, the program actually runs in bar\bin\debug (or bar\bin\release), so you need to account for that.

Typically I'll do something like:

#if DEBUG
filename = @"..\..\baz\asd";
#else
filename = @"baz\asd";
#endif
Eric J.
This will not work outside of Visual Studio if the file is not copied to the output directory.
sixlettervariables
For release mode, my installer copies all of the files that the program needs. I don't use the built-in installer (I use NSIS). Does that one require the file to be marked as content for it to be picked up?
Eric J.
Yeah, the installer needs them as Content and to be copied.
sixlettervariables
+4  A: 

You want to open the file in bar-project at runtime in a C# application? There are a few ways to accomplish this, and I'll give you the easiest.

  1. Go to the Properties for asd in Solution Explorer
  2. Set Build Action to Content
  3. Set Copy to Output Directory as Copy always (or Copy if newer)

You can then reference the file as follows in your C# (assuming your program does not change the current working directory away from the Executable Directory):

using (StreamReader reader = new StreamReader(@"baz\asd"))
{
    // ...
}

The other ways involve using embedded resources, which is not the most succinct of solutions.

sixlettervariables
Copying this type of file to the output directory leaves you with two copies on disk. This is fine if the files are small and few.
Eric J.
I agree, but it is the easiest. Regardless, embedding the resource rather than leaving it on disk still takes up space.
sixlettervariables
I don't copy or embed while debugging, just leave the files where they are. For release builds, my installer (I use NSIS) is responsible for installing the required files on the target machine.
Eric J.
Fair enough, we have used NSIS in the past but we've still used the contents of the Output Directory as the list of files to include in the installer.
sixlettervariables
+2  A: 

The relative location of the file is to the solution dir. So, to open it in the VS IDE, you can type in the Command window or the Find textbox on the Standard toolbar (Ctrl-D):

>open bar\baz\asd

If you want to do it at runtime, you need to distribute the source files with your application, as nothing guarantees it'll be executing anywhere near the solution tree.

If you want to do it from a VS Add-In, I am sure there's a class representing the solution, which you can use to enumerate the projects within it and then enumerate the file entries in the bar project.

Franci Penov