views:

385

answers:

4

I am writing a small command-line tool for my own daily tasks, and having problems reading from a XML file I have used for configuration. As per the examples, I use this code to load the XML file for Linq-to-XML.

    XDocument doc = XDocument.Load("SearchSources.xml");

What I'm having problems with is when I "deploy" my app and XML to c:\windows\system32 for easy access, it won't work when I try to launch the file from the RUN prompt (e.g. run => TOOL -commands) because it's looking for the XML relative to wherever I launch the application.

I could obviously change the path to be the full path, e.g. c:\windows\system32\SearchSources.xml in the code, but that would prevent me from running it via F5 in Visual Studio.

EDIT: I am attempting to do this in code, rather than modifying configuration files when I deploy the app to other locations.

A: 

Sounds like you need a config file with the path to the xml file in it. At install time you could modify the path if required.

Burt
I guess I'm not really trying to "install" the app, it's mainly for my use so I don't really want to have to modify any configuration settings when I copy it to where I want it.
MattGWagner
+6  A: 

Use:

String filePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().Location
) + @"\SearchSources.xml";

That will create a path to the file based on the directory of the executable.

Or using Path.Combine, as suggested:

String filePath = System.IO.Path.Combine(
    System.IO.Path.GetDirectoryName(
         System.Reflection.Assembly.GetExecutingAssembly().Location
    ),
    "SearchSources.xml"
);
Kazar
+1, I deleted mine, this is precisely it for getting your current app's path.
Abel
Better yet, avoid backslash heartaches with Path.Combine -- System.IO.Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, "SearchSources.xml");
Steve Gilham
The second response is slightly off, it needed to have the GetDirectoryName like the first response, or else it will retrieve .\Tool.exe\SearchSources.xml . The first response worked, thanks!
MattGWagner
Whoops, my bad, amended.
Kazar
+1  A: 

Try doing this:

var myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var file = Path.GetDirectoryName(myAssembly.Location) + "\\SearchSources.xml";

This will get the location of the current executable, then build a path from the executable's folder and the name of the file you're after.

d4nt
A: 

Create a Settings file in Visual Studio (Right click project, add-> new item -> Settings file ). There you can create a string with the path name to your file. In code, you can access it like this:

Properties.Settings.Default.MyString

This will create an xml file (app.config). I would then store the path in there, and use that in my app. That way, when you deploy it, you can just open the XML file in any text editor and edit the path.

BFree
I was really trying to get this done in code so I wouldn't need to modify anything when I move my application around. Thanks for the response!
MattGWagner