Why am I getting a "Could not find a part of the path" exception?
Asked Answered
S

8

15

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();
}
Shaylashaylah answered 11/9, 2013 at 11:57 Comment(7)
the error is clear enough, directory or path does not exist.Avril
Check whether 'C:\Inetpub\wwwroot\Vendor\cn.jpg' directory is in your machine.Trinl
Does your app pool identity have permissions on that folder?Punctual
The path is exist on my System I have host the application on server and access it from thereShaylashaylah
Because it search in the server not in your system.Preface
May be you need rights to access, Impersonator etc?Homoeo
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)Alluvial
D
8

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);
Diagnosis answered 26/8, 2015 at 6:19 Comment(0)
M
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.

Mathia answered 20/12, 2018 at 0:34 Comment(1)
This got me, was trying on a mapped drive.Giverin
D
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.

Doctrinaire answered 11/9, 2013 at 12:5 Comment(0)
H
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.

Halakah answered 11/9, 2013 at 12:0 Comment(0)
H
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.

Homoeo answered 11/9, 2013 at 13:29 Comment(0)
R
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.

Rheinlander answered 2/7, 2015 at 4:59 Comment(0)
V
0

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

Vasquez answered 5/8, 2024 at 13:43 Comment(0)
D
-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.

Danu answered 11/9, 2013 at 13:33 Comment(1)
How is this even related to question?Boater

© 2022 - 2025 — McMap. All rights reserved.