1

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!

1
  • Are u using the FileUpload control of Asp.Net Commented Apr 12, 2010 at 11:56

2 Answers 2

1

Try using Server.MapPath. This is much better to use in Web Apps.

HTH

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Raja, but this didn't work for me. The file path I'm trying to get is simply: C:\input_file_contacttype.mdf But 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
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, 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);
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: forums.asp.net/p/1077850/1587461.aspx HTH
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.
1
string fullpath = Request.PhysicalApplicationPath;

4 Comments

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\\" string So how do I just return c:\ directory?
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, could you read the response I gave Raja? Do you know how to solve this?
can you try this? System.IO.Path.Combine(Server.MapPath(), "subdirectory\filename.ext"); substituting applicable subdirectories and the filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.