views:

346

answers:

2

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.

+8  A: 

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

Will
+1  A: 

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.

Isotech
Thank you! Now, on a related note, how do I get the selected item on a double click for a listbox? I thought it would be listbox.SelectedValue but that's returning null when I do a double click. Thanks again!
geoff
ListBox.SelectedItem
Will
Thank you! I thought it'd be the selectedvalue.tostring().
geoff