简体   繁体   中英

double click to open file c#

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.

将文件名传递给System.Diagnostics.Process.Start()方法

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM