DotnetSparrow
DotnetSparrow

Reputation: 27996

HttpContext.Current.Server.MapPath alternative in windows application

what is alternative of "HttpContext.Current.Server.MapPath" in Windows application, which is used in web application. What should I use in windows application to access a file.

[EDIT] What is alternate of this in window application?

reader = XmlReader.Create(
            @"D:\EnviroTrack\EnviroTracker.Web\TestDevice\Data.xml", settings);

Please suggest

Upvotes: 2

Views: 9608

Answers (2)

keyboardP
keyboardP

Reputation: 69362

You can use the normal Path methods. MapPath helps convert the virtual path to the physical path on the web server. There's no need for this conversion in winforms. You may be looking for Assembly.GetExecutingAssembly().Location which returns the location of the assembly being executed.

Edit - Your updated question should work on in a Winform. XmlReader.Create has quite a few overloads, one of them is (string, XmlReaderSettings). This is the overload you're using in your question. You can use the same method, but different directory if you like.

reader = XmlReader.Create(@"C:\Data.xml", settings);

To get the directory of the executing assembly, you can use AppDomain.CurrentDomain.BaseDirectory. So it could be something like this:

reader = XmlReader.Create(AppDomain.CurrentDomain.BaseDirectory + "Data.xml", settings);

Upvotes: 3

jasons
jasons

Reputation: 1

I think Assembly.GetExecutingAssembly.Location returns the exe or dll name , haven't tried this functionality in a while, so can't be certain about it.

Application.StarupPath might do the job.

Upvotes: 0

Related Questions