Reputation: 6337
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();
}
Upvotes: 16
Views: 173639
Reputation: 1
If you are running a program locally, but are referencing a drive letter on a server, you will get this message. For example, you probably do not have a G drive locally. The program will run on the server, but not locally. So to write to a folder on the server and when running locally, use \servername\folder1\file.txt instead of G:\folder1\file.txt
Upvotes: 0
Reputation: 7769
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);
Upvotes: 8
Reputation: 1875
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.
Upvotes: 5
Reputation: 122
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.
Upvotes: 0
Reputation: 18759
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.
Upvotes: -4
Reputation: 3661
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.
Upvotes: 0
Reputation: 1325
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.
Upvotes: 1
Reputation: 4081
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.
Upvotes: 0