Share via


Solving the Path does not exist problem

Question

Wednesday, November 28, 2012 6:40 AM

Hi

I get an erratic problem when I'm using either Microsoft.Win32.OpenFileDialog() or File.Copy. In the dialog I will navigate to a file but when I double-click it it will say "[File] Path does not exist. Check the path and try again". This occurs even if I set the property CheckPathExists to false.

If I use a drag'n'drop method to get the file path, when I copy the file (File.Copy) it throws an exception with message "Could not find a part of the path [FullFilePath]".

Yet in both situations I can navigate to it fine with the dialog and windows explorer. What would cause this problem?

Further info:

- This does not happen all the time.

- The file is on a network drive W:\LocationOfFileFolder that is mapped to an ip \10.4.3.30\Folder\Folder\LocationOfFileFolder. Could it be that copy can't map properly? Do I need to somehow get the full IP in the path? However this copy occurs on the clients application.

- The client is the "owner" of the file, so there is not an issue with permissions.

Any help would be greatly appreciated.

All replies (17)

Sunday, December 2, 2012 10:57 PM âś…Answered | 1 vote

To all who replied, thanks. Frustratingly, the source of the problem was caused by development in a different area of the program that did Windows Impersonating and didn't dispose of the impersonated user. Thus although the user account had access to the folder, the impersonated user didn't, causing these errors.


Wednesday, November 28, 2012 8:15 AM

Hi, stevek.

I remember the network drive uses the NetBIOS protocol , but I feel it isn't stable. So you got that problem.

You may try the "net use" command on the computer which has the network drive.

At first, you type "net use" in cmd, and look whether path of the network drive shows in the cache list.

if not, you type the command below:

net use \10.4.3.30\Folder\Folder\LocationOfFileFolder "<password>" /user:"<username>"

You also see the "net use" command help.

After finishing the steps above, the computer'll remember the ip address.

But it isn't a perfect solution, because it still depends on your network.

If you can modify the program, you may copy file or transfer data under FTP protocol. 

FTP is very stable, at the same time, .net framework provides System.Net.FtpWebRequest Class too.


Wednesday, November 28, 2012 9:39 AM

when I double-click it it will say "[File] Path does not exist. Check the path and try again"

What is "it"? The dialog?


Wednesday, November 28, 2012 11:46 PM

"it" is the file.


Wednesday, November 28, 2012 11:47 PM

Thanks for your suggestion. I'll look into FTP protocol.


Thursday, November 29, 2012 12:33 AM

Thinking about this more, even if I do use FTP protocol I still need locate the file, and Microsoft.Win32.OpenFileDialog is not letting me do that. Even though I set CheckPathExists to false and locate the file it still says the path doesn't exist?!


Thursday, November 29, 2012 2:12 AM

Hi, stevek.

When you use ftp, you need to add a new form instead of OpenFileDialog.

When it's loaded, you get file list from ftp server.

When a user open one file, you download the file from ftp server.

When a user copy one file, you read the file stream and upload it to ftp server.

I find an example for you. OpenFileDialog for FTP

I hope it's helpful to you.


Thursday, November 29, 2012 3:27 AM

Thanks for your help a_Neils - I hadn't thought about FTP before.

Because it would involve forgoing drag'n'drop I'm inclined to make it plan B or C - still hoping for a plan A at this point.

aside: I forgot to add in my last reply, I don't think net use would help as the folders are frequently changing.


Thursday, November 29, 2012 3:29 AM

Can you show the code around where you display the dialog and use the result?

"Premature optimization is the root of all evil." - Knuth

If I provoked thought, please click the green arrow

If I provoked Aha! please click Propose as Answer


Thursday, November 29, 2012 5:23 AM

Sure, the first code block is the OpenFileDialog. So obviously the error with it is preventing ofd.ShowDialog from closing.

var ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Multiselect = true;
ofd.CheckPathExists = false;
ofd.CheckFileExists = false;
ofd.AddExtension = true;
if (ofd.ShowDialog() == true)
{
    foreach (string flname in ofd.FileNames)
       AddFile(flname);
}

The second code block is inside a background worker in the above AddFile() method. This method is called by both the above code block, and the drag'n'drop functionality. So ofcourse when the Dialog is saying "Path not found" it never gets to it, but the drag'n'drop still works up until the last line (File.Copy) where it throws an exception with message "Could not find part of the path".

var attachment = new Attachment()
{
    Filename = Path.GetFileName(flname),
    OriginalFilename = Path.GetFileName(flname),
    FullFilename = flname,
    DateUploaded = DateTime.Today,
};

if (!Directory.Exists(@"Temp"))
    Directory.CreateDirectory(@"Temp");
var pathOfTemp = Path.Combine(@"Temp", attachment.OriginalFilename);
if (File.Exists(pathOfTemp))
   File.Delete(pathOfTemp);// Delete file in Temp folder if it exists
File.Copy(attachment.FullFilename, pathOfTemp);// Copy to Temp

Thursday, November 29, 2012 7:26 AM

Hi,

Catch this exception:

**DirectoryNotFoundException **

**IOException  **

Also , please see :

http://support.microsoft.com/kb/827421

Regards.

Please remember to mark the replies as answers if they help and unmark them if they provide no help , or you may vote-up a helpful post


Thursday, November 29, 2012 1:25 PM

"it" is the file.

I doubt it. I have never seen a file saying anything. What is saying the file doesn't exist? Is it a messagebox? Are you getting an exception? If an exception, on which line?


Thursday, November 29, 2012 10:49 PM

Hi,

Louis, Here is an attachment showing the dialog and message that occured when the user double-clicked (or selected it and clicked Open on) the INV_... file. Does that clarify it?


Friday, November 30, 2012 12:02 AM

Thanks for your reply murtazagandhi.

I catch the exception when it occurs on File.Copy, but ofcourse that doesn't achieve my goal of copying the file :)

Regarding the link you posted - that is a long work-around! What has got me doubting that is the issue is that it doesn't happen for every file, and the "cause" listed seems to indicate the user would never be able to copy a file. The problem I have occurs sporadically, sometimes on files created today, sometimes on files created last week. And sometimes a restart fixes it.


Friday, November 30, 2012 6:56 AM

Hi,

This event occurs asynchronously,,

There s a lock till the path is being used,, it is not accessible ..

So you have to make the Addfile() function asynchronous and

invoke using a delegate , using begininvoke,endinvoke,

and use lock on object to protect cross-thread reference..

like

Pseudo Code:

Object obj  = new Object();

Thread :

Addfile()

{

try

{

lock(obj)

}

catch

{

}

}

Regards.

Please remember to mark the replies as answers if they help and unmark them if they provide no help , or you may vote-up a helpful post


Monday, December 3, 2012 1:55 AM

Hi stevek,

I am glad to know you have solved this problem and thanks for sharing the solution. This is helpful for other developers in the forum who have interests in this problem.

Have a nice day!

Regards,

Lisa Zhu [MSFT]
MSDN Community Support | Feedback to us


Friday, September 11, 2015 1:05 AM

There is definitely something wonky going on with UNC paths and mapped drives with within the ShowDialog() method when the calling code is running as an impersonated user.  Even with granting the impersonated user full access to the file and the path, after the file is selected in the dialog, clicking the dialog's OK button throws an alert message box.

[Window Title]
Open <your customization>

[Content]
<selected FileName without the extension>
Path does not exist.
Check the path and try again.

[OK]