2

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

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
  • possible duplicate of [HttpContext.Current.Server null](http://stackoverflow.com/questions/6304532/httpcontext-current-server-null) – Dan Abramov Jun 11 '11 at 01:17

2 Answers2

3

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);
gp.
  • 8,074
  • 3
  • 38
  • 39
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • @DotnetSparrow - Updated. You can use the BaseDirectory property which will return the path of the executable. You can then add your `Data.xml` file relative to this path. – keyboardP Jun 10 '11 at 18:23
0

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.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
jasons
  • 1
  • 1