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)
Related
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.
I am using windows application to send mail with the attachment. But at this line of code I get error as
The name 'server' does not exist in current context
var directory = Server.MapPath("Attachment/");
I tried from here but it was not working for me
You are trying to use a Server object in a windows application, the Server object is for web applications only.
You can simply specify the full local path to the directory and use this, unlike having to map it based on a virtual directory as in a web application.
If the path to your files/directory are contained in the same folder as your application, you can use Application.StartupPath property and combine this with your own values to get a full path.
var directory = Application.StartupPath + "\\Attachment\\";
I have an xml named Mapping.xml stored in my local system. i am accessing in code ,in my local system like XDocument xd = new XDocument(#"D:\MVCPopup\Mapping.xml"); .But i don't think this will work if i deploy it in iis server due to the folder structure change.Do we have a generic mechanism which will find the file Mapping.xml. Will Server.Mappath will work here?
Server.MapPath returns the phisical address from a virtual one. So if you configure a virtual folder on the iis that maps to the folder you save your XML in, then yes, Server.MapPath will help.
I have written a server in C# for a JS client.
The Solution consists restApi BL and DAL.
The BL creates links for images stored on a virtual directory, on the server.
The JS and the server code, are stored in the same directory.
When I build the string of the link I use this line of code:
string keyImageName = Settings.Default.ServerUrl +
Settings.Default.KeyImagesFolder + relatedFinding.KeyImagePath;`
where KeyImageFolder is a virtual directory.
It works fine, but my problem is that the website has multiple Amazon instances, one for each geographical zone , so every time I deploy, I need to change the ip in the settings.it's annoying.
Is there a way to get the virtual directory's url, specifically for each machine?
if the JS is installed on the same machine as the server, does it really need a full path?
Many thanks
First, you'll need to get the physical path for the file or directory that you want to generate a url for. This can be done within a Page object using Request.ApplicationPath.
Next, this path can be converted to a url path using the Server.MapPath function. This will take into account if there are more than one websites tied to the same path in IIS.
var physicalPath = Path.Combine(Request.ApplicationPath, Settings.Default.KeyImagesFolder, relatedFinding.KeyImagePath);
var resourceUrl = Server.MapPath(physicalPath);
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"));