简体   繁体   中英

C# - Open a file with the associated program without invoking the command line

Is it possible to open a file with the default program without invoking the command line? I want to run a unit test and have the unit test open the file (PDF) at completion for visual inspection.

Just call Process.Start(filePath) .
This will open the file in the user's default program.

我认为这应该有效:

System.Diagnostics.Process.Start(@"c:\file.pdf"); //i.e provide the full path!

只需使用以下语法:

System.Diagnostics.Process.Start(@"c:\yourfile.txt");
 Process process = new System.Diagnostics.Process();
 process.EnableRaisingEvents = false;
 process.StartInfo.CreateNoWindow = true;
 process.StartInfo.FileName = filePath;
 string arguments = fileArguments;
 process.StartInfo.Arguments = fileArguments;
 process.Start();
 process.WaitForExit();

This way you can invoke and put the file name in the pdf with parameters/arguments. You can also specify different programs and put it in the path, then the pdf name in the fileArguments. It's up to you.

if you use this code:

System.Diagnostics.Process.Start( "C:\...\...\myfile.pdf" );

the pdf should get opened by the default program associated to the .pdf extension.

is this what you wanted? I would be careful in putting this inside the unit test in case you include those tests in an automated build on the server, which runs with no logged in user, this could be an issue if it fails and if it does not fail, who is there to close Acrobat Reader? :D

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