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
Related
I am trying to do is to get filepath for my excel file. But I am unable to do so.
File is in Document/Visual Studio 2013/Project/ProjectName/a.xlsx
string path = Path.Combine(HttpContext.Current.Server.MapPath("~/"),"a.xlsx");
string SheetName="Sheet1";
Is it wrong way to do it or is it correct way?
This is the better answer according to me.
Better to save in
C:\Users\AJ1110\Documents\Visual Studio 2013\Projects\Proj\Proj
And in
program.cs
string pathfile = #"..\..\a.xlsx";
string sheetName = "Whatever_SheetName_IS!!!";
This might solve your problem.
HttpContext.Current does not work outside of a web context.
If your project is running inside a console or windows program, it cannot work with HttpContext.Current. MapPath is meant to translate a web path to a file system path. ~/ is a .Net convention for pointing the root web path of a web application.
You should explicit what are your requirements about how to resolve the folder containing your file.
Maybe should you simply put that in some configuration file (using settings property tab of the project by example) and retrieve it from there.
Edit:
So, from your comment on this question, it looks like you have to seek the xl file in the executing folder.
There is a number of ways for achieving this, depending on your application use cases.
By example, check this question.
Since your project is not a Web one, I expect that you some sort of Output where build process generates an executable file, some assemblies etc. You can put Build action of your Excel as Content (more details here) and use this base path to retrieve it:
System.Reflection.Assembly.GetExecutingAssembly().Location
It is important to think in terms relative to your executable (or executing assembly to be more precise), since your output will have to run outside your development environment and your excel must still be accessible.
Also, getting the exact executing assembly might be tricky in some scenarios.
I created a winform application and then created a setup of that application. this application records some info at Application.StartupPath in a file. unfortunately i got exception when i try to write the file 'Access to is denied'. Please guide me how can i get rid of that..
Thanks
You should never assume that the application startup path is writable by anyone besides system administrators, especially on modern Windows systems.
Instead of storing your file there, I'd suggest you use the folder returned by Environment.GetFolderPath(SpecialFolder.ApplicationData). That folder is guaranteed to be writable by the current user.
You can find the Microsoft guidelines about this issue here.
That's typical - you shouldn't be writing to the "Program" area of your application. You should be writing to a data area of the file system - perhaps the user's settings area, or a common application settings area.
Basically the policy was toughened up (in Vista, I believe) to try to discourage programs from doing exactly what you're currently doing. The best approach isn't to work round it - it's to change where your application stores its settings.
I've got a site setup in IIS to run at http://localhost/WebApplication6. In my web application I have a handler (implements IHttpHandler). When I print context.Request.Url.AbsolutePath, I get /WebApplication6/whaetever. I want to trim off /WebApplication6 (the local site name). How can I do that? Is there a way to get the "WebApplication6" bit so I know what to trim off? (inside IHttpHandler.ProcessRequest).
Your best bet would be HttpRequest.AppRelativeCurrentExecutionFilePath - it provides path relative to your web application root directory. However, it will be in form of "~/whatever" where ~/ indicates app relative path. If your requirement is to get /whatever then you can strip off ~ using string functions.
BTW, here's good article that will help you make sense of all paths: http://www.west-wind.com/weblog/posts/132081.aspx
VirtualPathUtility.GetDirectory(context.Request.Url.AbsolutePath)
I'm converting a windows forms application to a WPF application. Is there a way to obtain things like, Startup Path, User App Data Path, Common App Data Path, etc. without referencing System.Windows.Forms?
Previously, I used System.Windows.Forms.Application.StartupPath, but the System.Windows.Application.Current object doesn't contain the same information.
You might want to look at System.Environment.GetFolderPath.
The values of the SpecialFolder enum are numerous:
ApplicationData
CommonApplicationData
CommonProgramFiles
Cookies
Desktop
DesktopDirectory
Favorites
History
InternetCache
LocalApplicationData
MyComputer
MyDocuments
MyMusic
MyPictures
Personal
ProgramFiles
Programs
Recent
SendTo
StartMenu
Startup
System
Templates
Is that helpful?
this will help
for Application.StartupPath use AppDomain.CurrentDomain.BaseDirectory
http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory.aspx
You can use the Environment class to get all types of system information. For directories, use the Environment.GetFolderPath method.
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
I understand and agree that referencing to System.Windows.Forms.dll looks ugly but I think that's better then decrease code re-use and re-invent bicycle instead just one more reference.
I use for example System.Windows.Forms.Application.ProductVersion in my console apps, don't like that but think that's the lesser evil..
See also this question on SO
Imagine I have a picture viewer application made with C# and .NET. I have already set the preferred application to view pictures to use the C# application.
I want to somehow let my program know where it has been invoked. How can I achieve this?
If you're using it to view pictures via shell associations, you can just check the picture filenames passed in on the command line. You can use Environment.GetCommandLineArgs to get the first filename:
// Should check to make sure there is at least one filename passed first...
string imageFilename = Environment.GetCommandLineArgs[1];
string directory = System.IO.Path.GetDirectoryName(imageFilename);
If you want the working directory, just check Environment.CurrentDirectory at startup...
I think you can use Environment.CurrentDirectory
The current directory (Environment.CurrentDirectory) of an application can change during execution. Additionally, the current directory may not be the directory in which the application resides, such as if a user runs it from a command line in an arbitrary directory by specifying an absolute path to the executable.
If you really want the "current directory" of the application, then use Environment.CurrentDirectory, but if you want to know the location of the application, you can use the following approaches:
System.Windows.Forms.Application.ExecutablePath
(if running a WinForm application)
System.Windows.Forms.Application.StartupPath
(if running a WinForm application)
System.Reflection.Assembly.GetEntryAssembly().Location