Find specific files in web project [duplicate] - c#

So I have a web project, and I'm trying to get the root directory of the website using the c# method Directory.GetCurrentDirectory(). I don't want to be using a static path as the file locations will be changing in the future. This method is running in my imageProcess.aspx.cs file, but where I thought it would return:
C:\Users\tcbl\documents\visual studio 2010\Projects\ModelMonitoring\ModelMonitoring\imageProcess.aspx.cs
I'm instead getting:
C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\
Can anyone explain why this is happening and what a possible solution might be? Thanks a lot.

The current directory is a system-level feature; it returns the directory that the server was launched from. It has nothing to do with the website.
You want HttpRuntime.AppDomainAppPath.
If you're in an HTTP request, you can also call Server.MapPath("~/Whatever").

Use this code:
HttpContext.Current.Server.MapPath("~")
Detailed Reference:
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".") returns the current physical directory of the
file (e.g. aspx) being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the
application
Server.MapPath("/") returns the physical path to the root of the
domain name (is not necessarily the same as the root of the
application)
An example:
Let's say you pointed a web site application (http://www.example.com/) to
C:\Inetpub\wwwroot
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
D:\WebApps\shop
For example, if you call Server.MapPath in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
then:
Server.MapPath(".") returns D:\WebApps\shop\products
Server.MapPath("..") returns D:\WebApps\shop
Server.MapPath("~") returns D:\WebApps\shop
Server.MapPath("/") returns C:\Inetpub\wwwroot
Server.MapPath("/shop") returns D:\WebApps\shop
If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path.
If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.
Note: in C#, # is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.
Footnotes
Server.MapPath(null) and Server.MapPath("") will produce this effect too.

For dot net 6 I use:
AppContext.BaseDirectory
Cool thing about that is that it will be the same on asp.net and also on a console application.

Related

C# Windows Service Enviroment.GetFolderPath() returning empty string

The question is pretty straight forward, I am making a Windows Service Program and the enviroment.getfolderpath isnt working.
Here is the code I have
string savePath = AppDomain.CurrentDomain.BaseDirectory; // this works
string savePath2 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // this returns an empty string...but why?
The documentation says
The path to the specified system special folder, if that folder
physically exists on your computer; otherwise, an empty string ("").A
folder will not physically exist if the operating system did not
create it, the existing folder was deleted, or the folder is a virtual
directory, such as My Computer, which does not correspond to a
physical path.
When running the Service as a Local System it doesn't run with any specific user permissions. Hence the GetFolderPath is returning empty because it is not able to recognize the path Desktop for LocalSystem.
You can either use Environment.SpecialFolder.CommonDesktopDirectory which will give C:\Users\Public\Desktop or
run the service with a specific user (in my case it's sampleuser) which will give the output as C:\Users\sampleuser\Desktop

When to use Server Path

When i start to attach the same file to AdRotator object, it didn't work :
AdRotator1.AdvertisementFile = Server.MapPath("~/Data/Ads.xml");
Then i used, and it worked:
AdRotator1.AdvertisementFile = "~/Data/Ads.xml";
Can anyone tell me when to use Server.MapPath?
From Microsoft: The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
So when you call Server.MapPath("~/Data/Ads.xml") it returns the complete path, including the directory on the server. But in your case, you probably just need the relative path, so you donĀ“t need to call Server.MapPath.
The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server

How to get the folder location on server with the domain name

I'm working on a c# web application which is hosted on an server 192.168.X.Y. In this application I'm accessing few documents which are inside a folder on a server 192.168.X.YYY\Folder(I'm providing this info in the Web.Config file). But the actual path of 192.168.X.YYY\Folder has a different name like abc.Application.com\Application1\Folder. How can I get this name in my c# application? Because I want to open the documents in this folder using google document viewer.
TIA :)
I used Request.ServerVariables["HTTP_HOST"] which served my purpose.
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".")1 returns the current physical directory of the file is being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

How to obtain the current executing directory irrespective of the host type

I have a code snippet that loads some assemblies from the current executing directory at runtime.
The code is part of a library that can be hosted in a console app/windows service/aspnet web app etc.
Is there a single API call that will provide the current directory the code is running from?
For a console/windows service AppDomain.CurrentDomain.BaseDirectory; returns the correct
path but the same call in an ASPNET app returns the path to the virtual root instead of the path
to the [virtualroot]\bin directory of the web app.
For ASPNET AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; returns what I want.
I could make a check like the following so that I get the correct path irrespective of the host:
string path = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath ??
AppDomain.CurrentDomain.BaseDirectory;
This sort of heuristic seems a bit hackish and I was hoping there is a single API call that will provide the expected results
To check where the current assebly is stored you can use this:
Assembly.GetExecutingAssembly().CodeBase
Or if your code is in a library and you wand the location of the started exe you can use GetEntryAssembly().
The codeBase is in URI syntax. To get the path (if required) you can use:
var path = new Uri(codeBase).AbsolutePath;

Loading an XML file in MVC ASP.NET from a server instead of the local drive path

I feel like I'm missing some configuration in IIS or Global file maybe ?
I want to load an xml file:
var icon = XDocument.Load("/images/body/logos/LogoImageMappings.xml");
When running it fails because it tries to locate the file # C:\images\body\logos\LogoImageMappings.xml
How can I correctly direct it to Localhost ( or web server when published ) application drive instead of a local c: ?
Thank you.
You have to map the path to the directory where the file resides. Use this:
string path = Server.MapPath("~/images/body/logos/LogoImageMappings.xml");
var icon = XDocument.Load(path);
The Server.MapPath function will map the relative path to the full path of the file on the server, replacing tilde with the right directory (note that the relative path must point to a sub-directory of your web-site directory, for security reasons usually you can't access anything higher than that).
You have virtual path, to get physical path use MapPath :
var icon = XDocument.Load(HttpContext.Current.Server.MapPath("~/images/body/logos/LogoImageMappings.xml"));

Categories