Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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.

like image 710
Worgon Avatar asked Feb 23 '23 06:02

Worgon


2 Answers

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.

like image 188
Niranjan Singh Avatar answered Feb 25 '23 01:02

Niranjan Singh


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?

like image 31
spender Avatar answered Feb 25 '23 00:02

spender