Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

like image 869
Philip Stuyck Avatar asked Oct 20 '25 02:10

Philip Stuyck


1 Answers

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

like image 137
user1385417 Avatar answered Oct 21 '25 17:10

user1385417



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!