Why am I getting the exception "Can't find part of the path"?

I am developing a website using Visual Studio 2010 . I am trying to save a file in a path. It works great locally.

But the same code does not work in IIS. It shows the following error:

Exception Details: System.IO.DirectoryNotFoundException: Failed to find part of the path "C: \ Inetpub \ wwwroot \ Vendor \ cn.jpg".

Could not find part of path "C: \ Users \ shashank \ Desktop \ ab.csv".

Here is the code:

 protected void btnImportFile_Click(object sender, EventArgs e) { sArReportText = File.ReadAllText(txtFilePath.Text.Trim()); // Set the report Properties to insert Report information SetProperties(); } 
+5
source share
7 answers

Perhaps you also have what I have: a directory name contains some unusual characters. In my case

 Could not find a part of the path 'C:\Web\metBoot\wild iis\DigiCertยฉ Certificate Utility for Windows_files'. 

This copyright mark is a problem.

So, using concepts made from Getting the short file name 8.3 from the long file name , I first convert my paths to short form and then use this to get the list of files.

  StringBuilder sf = new StringBuilder(300); int n = GetShortPathName(sourceFolder, sf, 300); if (0 == n) { tk.write(Marshal.GetLastWin32Error().ToString()); continue; } 

...

  IEnumerable<string> fileGroup = Directory.EnumerateFiles(sf.ToString(), ext); 
+3
source

Perhaps this is due to the fact that you do not have the specified file on the web server or you may use the wrong path. Indicate the exact folder and file name as it is stored on the web server. use HttpContext.Current.Request.ApplicationPath or Server.MapPath to indicate the correct location where the file is located. Also, make sure that you provide read and write permissions for this particular file and its folder.

+1
source

Permissions must be set in iis to allow saving files in a folder. Basically, your downloaded files should be saved in a separate folder located in the root directory.

0
source

To access, create and delete files, the server must have rights. As in my project, I use the Impersonator class to access various files and folders from the server. Otherwise, it will throw an exception.

0
source

You can use code impersonation:

http://csharptuning.blogspot.com/2007/06/impersonation-in-c.html http://www.codeproject.com/Articles/14358/User-Impersonation-in-NET

no matter who you use, because the avatar should be able to read / write to a place that is saved. We use this method in applications to delete / create folders over the network. Even if App_Data is best practice, business access to documents outside this folder may be required.

You can also set impersonation in IIS.

I also notice that your function is called btnImportFile. You might want to view the FileUpload control if you are uploading a file, which allows you to get an array of bytes of the file and save if necessary. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload%28v=vs.110%29.aspx . You may still have to use Server.MapPath or HttpContext.Current.Request.ApplicationPath depending on your needs.

0
source

See how you run VS too. Unusually, I only encounter this problem when I start VS in admin mode. Perhaps a group policy.

0
source

It is generally recommended that you use the App_Data folder to save files.

Look here. Working with files , for a tutorial.

-2
source

Source: https://habr.com/ru/post/1501642/


All Articles