Access to the path '\\Image0\Abc' is denied - asp.net

var directory = new DirectoryInfo("\\image0\Abc");
if (!directory.Exists)
{
directory.Create();
}
When I user above ASP.NET code which UNC path, I found the following problem:
Access to the path '\Image0\Abc' is denied.
How can I solve? Regards.

Add a Network Service account to that folder if it is located outside your web application folder. If it is, there might be a problem in the path that you are accessing.

Related

How to refer virtual path in asp.net?

I have created virtual directory in IIS under default web site. For example "VirtualPathTest". It refer the physical path "d:\samplelocation\VTest"
How to refer the "VirutalPathTest" in asp.net page to store file and retrieve files in that location?
The above path is not inside asp.net application.
Any one assist me to handle.
Thanks in advance.
You can achieve this by VirtualPathUtility
Ex: VirtualPathUtility.GetDirectory(Request.Path)
if you are looking to get the domain name associated with your current request, along with the virtual path of the current application, then try like this :
Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

HostingEnvironment.VirtualPathProvider.FileExists returns false on existing file

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
}

Read Write Permissions on IIS

I have create a directory uploads in the Content directory and inside the uploads dir i create a directory at the time of creation of user account. This setup works fine on the development setup but when i publish the site on my local IIS i get a access denied error.
Access to the path 'C:\inetpub\wwwroot\Content\uploads/d1bed3b7-f9e1-486a-953a-37abc7b99dad' is denied.
I have a shared hosting environment, the question i want to ask is, will i be able to set the permissions on the shared hosting environment?
What are the alternatives?
EDIT
here is code(tnx to #Marko) for creating the directory for the user
string UserGUID = Guid.NewGuid().ToString();
string path = HostingEnvironment.MapPath(#"~/Content/uploads");
Directory.CreateDirectory(path+"/" + UserGUID);
EDIT
after changing the code the error message has improved
Access to the path 'C:\inetpub\wwwroot\Content\uploads\ab569312-c487-4a08-bf49-99f7c69303f6' is denied.
There's a problem with the path, you have the wrong slash /
C:\inetpub\wwwroot\Content\uploads--------->/<----------d1bed3b7-f9e1-486a-953a-37abc7b99dad'
As long as the \uploads\ path has writable permissions any folders/files you create within that folder should inherit permissions from the parent.
EDIT
I'm not 100% sure why it works in cassini but change your code to this (it uses Path.Combine) instead of adding a slash manually (which is actually the wrong slash).
string UserGUID = Guid.NewGuid().ToString();
string path = HostingEnvironment.MapPath(#"~/Content/uploads");
string userPath = Path.Combine(path, UserGuid);
Directory.CreateDirectory(userPath);
If all of this fails, contact your hosting provider to set writable permissions to the /uploads/ folder or use the Administration panel if you have one.

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.

Resources