HostingEnvironment.VirtualPathProvider.FileExists returns false on existing file - asp.net

I'm trying to read a file in a virtual path, using the following code
HostingEnvironment.VirtualPathProvider.FileExists(_SiteMapFileName)
_SiteMapFileName has something like "~/Content/en-US/MainMenu.sitemap" and it is always returning false.
But if I access the URL of that virtual path (http://www.local.mysite.com/Content/en-US/MainMenu.sitemap), the file displays correctly in the browser
Content is a virtual directory under my website, pointing to a shared folder on a different location. If I access that location on Windows Explorer I can see the files correctly
I've set the site and the virtual directory to connect as my network account.
I used the process monitor tool to see if there's any problem and it says that w3wp.exe is trying to create the file \path\to\shared\location\Content\en-US\MainMenu.sitemap but it is getting ACCESS DENIED which is fine since it is a read-only location
Does anybody now how to solve this issue?

I found out the reason. The user set to run the app pool tied to the application needs to have permissions on the file is trying to read, not only the website and the virtual directory. :/

when i am working with vDir i use Server.MapPath
Does that not work for you?
if(System.IO.File.Exists(Server.MapPath("Relative Path to vDir")))
{
//do something with the file
}

Related

Files on network drive are not accessible when website is published on IIS

I have a website that shows a list of recommended documents retrieved from a network drive, Z drive. for each document, if the user pushes a button, he can navigate to the path of the file through windows explorer. below is the code.
string argument = #"/select, " + filePath;
System.Diagnostics.Process.Start("explorer.exe", argument);
an example of filepath is : z:\ecomarathon2012\04-Reports\fuel cell and batteries comparative.docx
Now, I have published the website on IIS but the button does not work anymore to show me the path of the file.
What changes I need to add in order to solve this problem for the published version of the website?
Sounds like your IIS user identity doesn't have access to the network drive. Give that user read access and I believe your code should work. If not, try to log some errors using try catch and post them here.

ASP.NET How to open a folder at server side?

I'm trying to open folder location from code behind
Process prc = new Process();
prc.StartInfo.UseShellExecute = true;
prc.StartInfo.FileName = #"\\Shared\FolderName\test";
prc.Start();
After that I got it I couldn't achieve by using Shell32 class or Process.Start on server side as well as adding link to path cant be used because of the security problem so how can achieve this problem ?
Thanks for your help
That's right, you won't be able to launch an external process in a web application (and with good reason!). I think you need to reconsider what it is you're trying to achieve... perhaps some more information will help us give you a better solution?
A web application hosted on server can not access external file and folders on the system. This is due to security reasons. However if you want to access some specific folder, you need to create virtual directory and give path to that folder to the virtual directory. Now you can access that folder using virtual directory.
I worked on such scenario where I needed to upload file using one application (admin app) and needed to show on an other application (client app). So it works fine if you want to access outside resources.

Downloading file using webclient results in "Access to the path denied is denied"

i have button, on a click of which i want to download the file on the local pc, i am using webclient.downloadfile(), but i am getting the below error:
Access to the path 'C:\Windows\SysWOW64\inetsrv\ms-banner.gif' is denied.
i am using below code to download file:
WebClient client = new WebClient();
client.DownloadFile(new Uri("http://www.contoso.com/library/homepage/images/ms-banner.gif"), "ms-banner.gif");
i dont understand why its fetching the file from local server, as i have already stated the remote uri
It's fetching the file from the remote server but trying to save it in the current directory because you have only specified a relative filename as second argument: "ms-banner.gif". And it seems that the account you are running your application under doesn't have permission to write to the current working directory which happens to be C:\Windows\SysWOW64\inetsrv.
So you have basically 2 possibilities:
Modify the account you are running your application under and grant it permissions to write to this directory
Specify another location (as an absolute path) to save the file to where the account you are running your application under has write permissions.
contoso.com redirects to microsoft.com... the path you have there in the URL does not exist and you won't be able to download it. I'm not sure why it's trying to go to your local machine, but have you tried some other image on some other website? Like http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif for example?

Upload files to different virtual directory

I have two web application hosted on different server.For eg. one is main application and the other one is branch application.
In branch application, user will upload their files but we want to keep all uploaded files in main application virtual directory.
So that we put the main application virtual directory path while we upload the files.But We got error message like "Invalid Directory" and can't upload.
Is there any way to upload files from one application to another directly? We are using normal html upload control in asp.net and visual studio 2008.
Code Sample :
main application virtual directory "http://10.10.10.1/mainapp/uploadedfiles/"
branch application virtual directory "http://10.10.10.2/branchapp/"
HttpPostedFile postedFile;
string saveFile = Path.Combine("http://10.10.10.1/mainapp/uploadedfiles/", "File1.pdf");
postedFile.SaveAs(saveFile);
Please guide me the right way and I really appreciated it.
Thanks.
Best Regards,
Chong
In the FileUpload control, the FileUpload.SaveAs(path) method takes a physical path, not a virtual path. So if you have a virtual path, you will need to use the Server.MapPath method to convert it to a physical path. Using your example, you might want to change the last line of your code to:
postedFile.SaveAs(Server.MapPath(saveFile))
Also, you will need to make sure that the account that ASP.Net is running under has ACL write permissions on the physical directory.
I think u can share the directory and ensure both the Apps can access it, because the two apps are in different servers, the directory path maybe just like : \serverA\Files\, and the url of this directory is : http://localhost/files. u can save files to other servers by a relative path if u have the permission.

ASP Classic can not access Virtual Directory using FileSystemObject on IIS 7

I have a Classic ASP website which we have moved from IIS 6 to Win2k8 and IIS 7. Within the website folder structure, is a Virtual Directory called Products containing JPGs that are physically stored elsewhere on the same server.
Within a web browser, any of the Product JPGs display correctly on the page. E.g. http://www.MySite.com/images/poducts/widget.jpg works a treat.
However, this folder is unavailable when trying to access it in ASP code, using the FileSystemObject - all other files/folders are there except the Virtual Directory. Here is an example ASP code snippet:
Set objFSO = Server.CreateObject( "Scripting.FileSystemObject" )
Set objBaseFolder = objFSO.GetFolder( Server.Mappath( "../../Images" ) )
For Each objFolder In objBaseFolder.SubFolders
Response.Write( objFolder.Name & "<br>" )
Next
Set objFolder = Nothing
Set objBaseFolder = Nothing
Set objFSO = Nothing
Additionally, Persit's ASPJpeg Com Object has no problem opening and saving JPG files to/from this Virtual Directory from ASP code.
In IIS7, the website has an Application Pool, and I've tried all manner settings for its identity to no avail. I have also tried applying various security settings (IUSR_, Network Service, et al) to the physical folder that the Virtual Directory points to - even granting full control to "Everyone" at one point.
It really seems like the ASP process does not have permission to Virtual Directories. If anyone has and idea on how to solve this problem then I'd be most greatful.
Using FileSystemObject to do this is never going to work because it only works on the physical file system. It does not know about or understand virtual directories - this aspect of your site is managed entirely by IIS.
It is not a question of permissions it is a question of the directory not physically being there so browsing the physical file system will never see it
IIS manages virtual directories:
Navigating to an image in your browser works because IIS automatically maps the virtual path to the appropriate physical path.
Using AspJpeg works most likely because it uses calls to Server.MapPath to resolve the given path into a physical path
This cannot be an issue of permissions since you stated yourself that AspJpeg can read and write to the virtual directory fine plus you can access it through your browser fine.
I vaguely remember having a problem like that and the culprit was Server.Mappath. To solve it I needed to map to a file inside the folder and then remove the file part
Server.Mappath( "../../Images/dummy.gif")
the "../" notation is not always allowed for security reasons. If you have access to IIS see if it is enabled or disabled.

Resources