Question

I have a listbox filled with file paths. Does anyone know how to open the default program for the file when it's double clicked? For example, if one of the items in the listbox says "c:\test.txt", how do you open it in notepad? And if it's "c:\inetpub\wwwroot\sitetest\test.asp" how can it be opened in the default asp editor? Thanks.

Was it helpful?

Solution

Pass the filename to the System.Diagnostics.Process.Start() method

OTHER TIPS

You can use the Process/ProcessStartInfo classes to execute the file with the default application handler in windows.

For Example:

ProcessStartInfo psi = new ProccessStartInfo();
psi.FileName = "myfile.txt";
Process p = new Process();
p.StartInfo = psi;
p.Start();

Keep in mind that p.Start() can throw exceptions you will have to handle, and different versions of windows will have slightly different behavoir. I know Win7/Vista will pop up the application selector dialog if there isn't a default handler for the file type, but in some versions, you will just get an exception.

This does not completely mimic a double click from windows explorer as is. For example with AutoCAD the correct version is loaded but there is a softlock license manager error when we send the filepath to Process.Start.

System.Diagnostics.Process.Start(dwgFilePath);

To fully emulate a double click from windows explorer we must pass the path to explorer.exe

System.Diagnostics.Process.Start("explorer.exe", dwgFilePath);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top