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.
I think this should work:
System.Diagnostics.Process.Start(@"c:\file.pdf"); //i.e provide the full path!
Simply use the following syntax:
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
精彩评论