0

I have an application that contains a sub folder that contain xml file ,that is use as a database now i want take the path of the xml file at run time ,how can i achieve this in window application?

I know how it does in asp.net using Server.MapPath but i want this is same in windows application

please help
thanks in advance .

4 Answers 4

2

Use Aplication.ExecutablePath property when am XML document and executable are reside in the same directory.

2

I think the recommended way in Windows is to use the Application.StartupPath property.

And with Path.Combine you can have your xml file path Server.MapPath-style like this:

var appPath = Application.StartupPath;
var xmlPath = Path.Combine(appPath, "data/my_db.xml");

// xmlPath now points to app-relative data/my_db.xml file

...

2
  • StartupPath can be different from the executable path.
    – Blue Toque
    Sep 20, 2009 at 18:23
  • It's a feature of Windows to allow usage of "Working Directories"... if you use Application.ExecutablePath you are essentially bypassing it.
    – chakrit
    Sep 20, 2009 at 21:12
1

A nuanced answer:

The best way to access data would be to put it in Application.CommonAppDataPath or Application.UserAppDataPath so that it does not depend on the application's installed path. However, there are many reasons why you might need to avoid this.

To answer your question:

  • If the application is a standard forms application deployed to the client's machine by an installer or XCopy deployment, then the path to the executable is Application.ExecutablePath
  • If the application is Click-Once deployed, then I would not recommend using the above since the app's path is obscured, shadow-copied and put in the sandbox. You can use ApplicationDeployment.IsNetworkDeployed to test for click-once deployment then ApplicationDeployment.CurrentDeployment.ActivationUri to get the URI that the application was launched from. Your app-relative file will be on that web server; you will always be able to download it.
  • if the application is a web app, then the Application class is useless. In this case you should use Assembly.GetExecutingAssembly().Location This works because currently executing assembly for a web app is almost always in the web app's /bin directory.
  • For a "portable" assembly where you don't have an installer, and for rare cases where you don't want to use the Application class, use Assembly.GetEntryAssembly().Location This works because it figures out what the entry point assembly is (your application) and uses that location. This is reliable because assemblies that your entry assembly load don't have to be in the same directory as the entry assembly.
0

You can get the directory of the currently executing assembly using

System.Runtime.Reflection.Assembly.GetExecutingAssembly().Location 

from there you can get to your subdirectory.

1
  • 1
    the currently executing assably may not be in the same directory as the application that called or loaded the assembly. A better way to do what you are suggesting is System.Runtime.Reflection.Assembly.GetEntryAssembly
    – Blue Toque
    Sep 20, 2009 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.