This should be really simple, but I have tried many solutions posted on the internet so I thought I'd give this a try here. I have a VS 2008 web application that I need to capture the full file path (directories and file name) from the selected file. So user selects a file and then clicks on one of the buttons which transfers control to my code for processing. So how do I get the file path? I can get the file name, but not the path. Thanks!
David, this didn't do what I wanted. It returns: Request.PhysicalApplicationPath "C:\\Documents and Settings\\Admin\\My Documents\\Visual Studio 2008\\Projects\\AddFileToSQL\\AddFileToSQL\\" stringSo how do I just return c:\ directory?
salvationishere
2010-04-12 12:39:07
see Raja's comment about the security exception. when you're debugging your app in VS, it's running in the path you mentioned. if you need access to a file in c:\, you'll need to specify it in the code or in your web.config
David
2010-04-12 13:08:47
David, could you read the response I gave Raja? Do you know how to solve this?
salvationishere
2010-04-12 14:38:08
can you try this? System.IO.Path.Combine(Server.MapPath(), "subdirectory\filename.ext"); substituting applicable subdirectories and the filename.
David
2010-04-12 16:47:38
+1
A:
Try using Server.MapPath. This is much better to use in Web Apps.
HTH
Raja
2010-04-12 12:00:17
Thanks Raja, but this didn't work for me. The file path I'm trying to get is simply: C:\input_file_contacttype.mdfBut your command returns: "C:\\Documents and Settings\\Admin\\My Documents\\Visual Studio 2008\\Projects\\AddFileToSQL\\AddFileToSQL\\input_file_contacttype.mdf" "C:\\Documents and Settings\\Admin\\My Documents\\Visual Studio 2008\\Projects\\AddFileToSQL\\AddFileToSQL\\input_file_contacttype.mdf" string
salvationishere
2010-04-12 12:37:55
You cannot access files outside your virtual directory since it would be a security exception. Otherwise you have to give explicit access for ASPNet account for that particular folder. So instead of access the file from C: it is better to access the file from your virtual directory. This would help when you deploy the site also.
Raja
2010-04-12 13:01:42
Raja, thanks for this useful tip. Just one more question now. I moved it to the above virtual directory and now I can see this full path listed in my Watch window. However, all of the strings I tried do not exist in this context. How can I fix these to store the full path into a string variable? This is what I have currently:string fullpath = Request.PhysicalApplicationPath; string fullpath2 = System.Web.HttpRuntime.AppDomainAppVirtualPath; string fullpath3 = Server.MapPath(uploadFile.PostedFile.FileName);
salvationishere
2010-04-12 13:37:45
Are you trying to upload a file? And are you trying to get the local path of the file? If then you cannot do it at server end. It strips off the path due to security reasons. Check out this link: http://forums.asp.net/p/1077850/1587461.aspx HTH
Raja
2010-04-12 14:41:19
Thank you for the article! Yes, I'm trying to do a file upload so I may have to try another solution if I want to enable Mozilla.
salvationishere
2010-04-12 16:58:03