views:

111

answers:

1

Hello, I'm trying to access files in a virtual directory I created in IIS for the first time but am getting weird results.

OS: Windows XP Pro

IIS: 5.1

Development Platform: Visual Studio 2008

Language: C#

Virtual Directory Name: portal

Local Path: C:\digital_communications_report_portal

Default Website Local Path: c:\inetpub\wwwroot

I can download the file at http://localhost/portal/testFile.xlsx without any problem.

I try either of these

string realFilename = Server.MapPath(ResolveUrl("~/portal/testFile.xlsx"));
string realFilename = Server.MapPath(ResolveUrl("localhost/portal/testFile.xlsx"));

and get "C:\Documents and Settings\jjohnson\My Documents\Visual Studio 2008\WebSites\clientsite\localhost\portal\testFile.xlsx" which is my project path with the virtual path slapped on the end and is not a valid path. I try taking the tilde or localhost and I get a "Failed to map the path '/portal/testFile.xlsx'." exception.

Any ideas what I'm doing wrong?

+1  A: 

If I'm not mistaken, Server.MapPath(ResolveUrl("~/relative/path/to/file")) is not what you want. It will produce a /rooted/path/to/file which when passed to Server.MapPath() will not yield a valid location.

Instead simply use Server.MapPath("~/relative/path/to/file");

Update

The problem you are experiencing is due to the fact that you are including the virtual directory name ("portal") in your relative URL. Your relative URL does not need to specify the virtual directory name, just the path to the desired file relative to the root of the application.

Nathan Taylor
That still gives me the same results as a the resolveurl...C:\Documents and Settings\jjohnson\My Documents\Visual Studio 2008\WebSites\clientsite\localhost\portal\testFile.xlsx
Jeremy
@Jeremy See my updated answer. The problem is that you are specifying the virtual directory name in the relative URL when you should not be. I have just tested and confirmed this on my own system.
Nathan Taylor
@Nathan, thank you so much for your patience on this but I'm still having trouble. If I understand you correctly, if I want to get to the folder /portal/help_desk (where portal is my virtual directory) I would be looking at Server.MapPath("~/help_desk"); but when I do that, I get C:\\Inetpub\\clientsite\\help_desk which is the path for the project with help_desk stuck on the end when I'm trying to get to C:\digital_communications_report_portal\help_desk (the root folder being where portal points to).
Jeremy
I'll tell you what I'm trying to do...I'm trying to display the folders and files in the virtual directory. The folders should be links that drill down to child folders and the files should be downloadable links. So in theory, I click on the help_desk folder and using a DirectoryInfo object, get a list of the folders in help_desk and with a FileInfo object, get testFile.xlsx. All members of these two objects get assigned to hyperlink tag. I have all the functionality working localy but when I try to run it through IIS as a virtual directory I start to get into trouble.
Jeremy
@Jeremy do you experience the same difficulties if you execute the application in a non-virtual directory- i.e. in the root of your web server?
Nathan Taylor
No locally, everything works fine...if I were working locally, the app is done! Unfortunately, I'm not so lucky. :)
Jeremy
Minor Update: Well, I messed around with Server.UrlPathEncode and was able to hardcode a link that gets to the testFile.xlsx for download. That works. So my problem seems to be just with the physical path for navigating the files and directories through DirectoryInfo and FileInfo. That's where I seem to be having problem with Server.MapPath.
Jeremy
Minor Update 2: string folderPath = Server.MapPath("/portal"); DirectoryInfo di = new DirectoryInfo(folderPath); actually gets me to the right folder now. {C:\digital_communications_report_portal} base {System.IO.FileSystemInfo}: {C:\digital_communications_report_portal}Exists: trueName: "digital_communications_report_portal"Parent: {}Root: {C:\} However, di.GetDirectories() gives "Access to the path 'C:\digital_communications_report_portal' is denied." And when I change folderPath to "portal/help_desk" and check di.exists, it says false even though I know it's there.
Jeremy
@Nathan, it looks like the final piece to the puzzle was simply permissions on the folder. I added aspnet as a user to the folder and all seems to be right with the world. Thank you so much for all your help, guidance and patience.
Jeremy
@Jeremy I'm glad you got it, I don't think I would have guessed that!
Nathan Taylor