Question

I am using this code for my LinkButton to download a file when you click on it. Recently i got this problem, i need a virtual path .

I would like to redirect to C:\inetpub\wwwroot

 string filepath = Server.MapPath("...");           
 FileInfo myfile = new FileInfo(filepath);

So I don't know what to put in Server.MapPath() because if I put / it will go to the base of my folder documents and no In

Was it helpful?

Solution

If you need to serve files from another part of your system then the best way to do this is to create a virtual directory.

In IIS right hand click on your website, click 'Add Virtual directory'. Give it a name and point it whereever you need it to be. E.g. new virtual directory called 'files' pointing at 'C:\inetpub\wwwroot\files'.

Then from within your site you can reference these files by using

/files/filename.txt <- the /files/ will link to your virtual directory.

You will then of course need to make sure you have your permissions set correctly to read these files but I will leave that up to you.

OTHER TIPS

You currently see a folder under your My Documents because you are running/debugging in your project folder, probably using the Development Server or IISExpress.

As soon as you deploy your site to a folder under C:\Inetpub, the MapPath will return that folder.

But, as a security measure, you cannot return a folder higher (in the tree) as the root folder from your application.

If you need that, you have to remove folders from the returned path yourself. You can use System.IO.Path for that.

You are not allowed to move up from your root. The root means the root folder of the application. If you are trying so, it is a security breach.

So if you wish to go to the root of the application you can use Server.MapPath("~");

Server.MapPath("~"); 

will give you current web application root.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top