简体   繁体   中英

Using HttpContext.Current.Server.MapPath in Window Application?

Can i do something like this in window Application?

HttpContext.Current.Server.MapPath("Email/ForgotPassword.txt"));

This project is a web based application.

And my next project is basically a window service...

Seeking for advice.

To get the path that the Exe is running at (which is a good location to add paths like "Email"), use:

string filePath = Application.StartupPath + "\\Email\\ForgotPassword.txt";

This path is the ..\\bin\\debug\\Email path when you run it on VS and ..\\Email path when you run it after installation.

There are few alternates to do this like these to access the directory path:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

or

using System.IO;
using System.Reflection;

string path = Path.GetDirectoryName(
                     Assembly.GetAssembly(typeof(MyClass)).CodeBase);

you can do manipulation with the Path class like get the full path, directory name etc etc..

Check this MSDN forum thread How to get the current directory path c# winfows for application for details.

As you expecting to do windows application as like web.. It is not possible.

If you're not processing requests from ASP.NET (or more specifically, System.Web ), HttpContext.Current will be null. So then the answer is: no, you can't do what you are asking. Perhaps:

Assembly.GetExecutingAssembly().Location

combined with methods from Path would be useful?

As other people suggested it is not possible to lift up an entirelly new HttpContex out of nothing in Windows Application. The reason is that while some of the things you can obtain, others are based on the fact that you are running in web environment (for example - what would you put in the Request / Response properties of the .Current context?).

Anyway there are some things you can do as Enumerate the IIS Web Sites / Virtual Directories, and find yours (sorry for the bad article code). Then you can obtain the directory for the website, or even Open website's Web.Config and read it (sorry for the bad code again).

Basically you are looking at the DirectoryEntry, WebConfigurationFileMap and VirtualDirectoryMapping classes although I am not quite sure where will you end up.

Still I will really ask you to review your architecture / question. It may be wrong from the start after you need the HttpContext in Windows application.

Maybe if you give us a bit more details we can help?

As mentioned by other HttpContex is not meant to be used in Window Application it is made for web application.

to simulate the HttpContext behavior in Window application you can declare global variable to use it in Application later.

also for current directory you can use Assembly.GetExecutingAssembly().Location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM