Philip Stuyck
Philip Stuyck

Reputation: 7447

Process.start results in file not found even though the file exists

I am using following code to open an arbitrary file with an arbitrary application that is installed on the system :

if (File.Exists(_document.DocumentFullPath))
{
    Process.Start(_document.DocumentFullPath);
}
else MessageBox.Show(string.Format("Document {0} does not exist!", _document.DocumentFullPath));

When I execute this code I see that the file.Exists returns true, so the file does exist. But then the corresponding application opens, for instance an image viewer or a pdf viewer depending on the file type (jpg or pdf) that I am trying to open, but it shows an error in that application that it cannot find the file. No exceptions are thrown. This does not happen all the time but only for some files. If I try to open these files manually by copy pasting the content of DocumentFullPath in the explorer, then the file does open correctly in the applicable application.

Duh I don't understand what is wrong. Example of a filename that don't work :

C:\Users\stuyckp\Documents\Visual Studio 2010\Projects\WPF\FrakoKlantOpvolgingWPF\FrakoKlantOpvolgingWPF\bin\Debug\ProjectDocumenten\11339_Wigbers\6197_koelkast \big-money.jpg

I am running on windows 10.

Upvotes: 4

Views: 7410

Answers (1)

user1385417
user1385417

Reputation: 131

Do you have spaces in the file path? File.Exists can handle spaces fine, presumably as can Windows Explorer, but it's possible that Process.Start cannot.

I think this is what you want bud. Use the process start using a ProcessStartInfo object with the file path with spaces passed as a string separately.

Use Process.Start with parameters AND spaces in path

Upvotes: 5

Related Questions