1

I was wondering if anyone here knows how to find the complete path (from the drive letter onwards) of a ContentManager instance. using this I could create a string with the right number of "..\" to append to the file path when I want to load a file from anywhere else on the computer (eg. from a registry key).

So basically I'm asking if there is a way.

annonymously
  • 4,708
  • 6
  • 33
  • 47

1 Answers1

0

You may just want to use System.GetFolderPath with one of these locations, most likely Program Files. From there, you can navigate to your application's installation directory. You could also use .Load("\MyFolder\blah") which will load from the default disk (e.g. C:\MyFolder\blah.xnb or whatever).

If you want the path of your .exe file...

using System.IO;
using System.Windows.Forms;

//blah blah
string GetAppDir()
{
    return Path.GetDirectoryName(Application.ExecutablePath);
}
GGulati
  • 1,027
  • 6
  • 11
  • I tried using "\MyFolder" but it doesn't work. I have no idea why but it throws an exception 'Could not open file', and I did copy the xnb file – annonymously Jan 02 '12 at 01:46
  • The file is called MyFolder in that case :P. You'd have to use "\\MyFolder\\file" in order to load file.xnb from the folder C:\MyFolder. Also be aware that Content.RootDirectory could be set to "\\MyFolder" and then you'd load Content from that folder normally (including sub-paths such as Textures\myTex or Sounds\powerup and so on) – GGulati Jan 02 '12 at 01:49
  • I was just illustrating what I meant the actual file is Egg.xnb. What I really need is a way to get the full root directory without reflection and without setting the RootDirectory property to "\\" – annonymously Jan 02 '12 at 01:54
  • Quick question: does your application have permissions to access random stuff on the C drive? If it doesn't, then that is the likely cause. It may require admin permissions/elevation (on Vista/Win 7) due to possible security hazard posed by such an application. Try loading a file from Program Files or your desktop (using System.GetFolderPath and all). – GGulati Jan 02 '12 at 01:56
  • I can open a text file with a stream in C:\ so I think I have permissions – annonymously Jan 02 '12 at 01:57
  • Interesting; not sure off hand without duplicating your program. Also, I amended my answer to account for my misunderstanding of your question. – GGulati Jan 02 '12 at 01:59
  • Ah, that was exactly what I wanted. Now I can combine that path with my content RootDirectory and then use that. Thanks :) – annonymously Jan 02 '12 at 02:02
  • Oh wait, that class `Application` is in `System.Windows.Forms` but I'm using XNA. – annonymously Jan 02 '12 at 02:03
  • You can still use it; just add the appropriate reference to your project. XNA depends on WF internally, so it's not like you're actually introducing new dependencies. – GGulati Jan 02 '12 at 02:19
  • I fixed it by adding the reference and NOT using the namespace because it clashes with a class in the XNA Framework. I used: using TApplication = System.Windows.Forms.Application; – annonymously Jan 02 '12 at 02:32