Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 578
Shashank Avatar asked Sep 11 '13 11:09

Shashank


People also ask

Could not find a part of the path mapped network drive?

This is occurs because the mapped drive in the path is not mapped under the user context that is executing the Windows Task. To resolve this problem, change the path in Sync & Save to use a UNC style path.

Could not find a part of the path in VB net?

If the error message says that it couldn't find part of the path then it couldn't find part of the path. You cannot create a folder and a file at the same time. You can only create a file in a folder that already exists. If the folder doesn't exist then you must create it first, then create the file.


2 Answers

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.

like image 146
Ben Power Avatar answered Sep 27 '22 20:09

Ben Power


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 convert my paths to short form first, then use 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);
like image 39
bugmagnet Avatar answered Sep 27 '22 22:09

bugmagnet