13

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

But the same code is not working in IIS. It shows the following error

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Inetpub\wwwroot\Vendor\cn.jpg'.

Could not find a part of the 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();
}
durron597
  • 31,968
  • 17
  • 99
  • 158
Shashank
  • 6,117
  • 20
  • 51
  • 72
  • 3
    the error is clear enough, directory or path does not exist. – Zaki Sep 11 '13 at 11:59
  • 2
    Check whether 'C:\Inetpub\wwwroot\Vendor\cn.jpg' directory is in your machine. – now he who must not be named. Sep 11 '13 at 11:59
  • 3
    Does your app pool identity have permissions on that folder? – geedubb Sep 11 '13 at 12:02
  • 1
    The path is exist on my System I have host the application on server and access it from there – Shashank Sep 11 '13 at 12:11
  • Because it search in the server not in your system. – Alessandro D'Andria Sep 11 '13 at 12:32
  • May be you need rights to access, Impersonator etc? – Incredible Sep 11 '13 at 13:27
  • You seem to have two separate filenames in the question pointing at very different paths (one of which of particular note is going to be heavily locked down permission wise to a single user). So is it `ab.csv` that you are having problems with or `cn.jpg`? Also the question text says that you are having trouble saving a file to a path but the code you have included shows a file read action... Can you mke sure you either describe your question properly or include correct code (or both) – Chris Sep 11 '13 at 14:16

7 Answers7

6

You might also be experiencing what I am: that the 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'.

That copyright sign is the issue.

So using concepts drawn from Obtaining the short 8.3 filename from a long filename, I converted my paths to short form first, then used that to get my 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);
John Smith
  • 7,243
  • 6
  • 49
  • 61
bugmagnet
  • 7,631
  • 8
  • 69
  • 131
5

Consider how you're launching VS too. Counter-intuitively I run into this problem only when I'm running VS in Administrator mode. Possibly a group policies thing.

Ben Power
  • 1,786
  • 5
  • 27
  • 35
1

This may be because, you are not having the specified file in web server, or you may be used an incorrect path. Specify the exact folder and filename as how it is stored in the web server. use HttpContext.Current.Request.ApplicationPath or Server.MapPath to specify the correct location where your desired file lies. And also make sure that you have given read and write permissions for this specific file and its folder.

Robin Joseph
  • 1,210
  • 1
  • 11
  • 12
0

You need to have permissions set in iis to allow files to be saved in the folder. Basically your uploaded files should be saved inside a separate folder present inside your root directory.

Bibhu
  • 4,053
  • 4
  • 33
  • 63
0

In order to access, create and delete files on the server, must have rights. Like in my project I am using Impersonator class to access various files and folder from the server. Otherwise it will throw an exception.

Incredible
  • 3,495
  • 8
  • 49
  • 77
0

You could use code impersonation:

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

regardless, whomever you use as the impersonation must be able to read/write to the location that is being saved to. We use this method in applications for delete/create folders across network. Even if App_Data is best practice, it may be a business requirement to access the documents outside of that folder.

You can also set impersonation on IIS.

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

jmcclure
  • 122
  • 11
-4

It's usually best practice to use the App_Data folder to save files to.

Take a look here, Working with files, for a tutorial.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82