简体   繁体   English

Process.start 以应用程序和文件名作为变量

[英]Process.start with application & filename as variables

Have been searching, but surprisingly could not find this specific question:一直在搜索,但令人惊讶的是找不到这个特定的问题:

With C# I want (by clicking a button in a form) to run a certain file, with an certain application.使用 C#,我希望(通过单击表单中的按钮)使用某个应用程序运行某个文件。

When using "Process.start(variable)" I can only pick one of the two.使用“Process.start(variable)”时,我只能选择两者之一。

And by using "Process.startinfo.filename" (like: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.filename?view=net-5.0 ) this also seems to be the case.通过使用“Process.startinfo.filename”(如: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.filename?view=net-5.0 ),这似乎也是案子。

Isn't is possible to just combine both in some "easy" way?不能以某种“简单”的方式将两者结合起来吗?

Thanks.谢谢。

Typically you would run a file with an application using a command argument (ie 'notepad.exe file.txt').通常,您会使用命令参数(即“notepad.exe file.txt”)运行带有应用程序的文件。

If that is possible with the application(s) you are attempting to launch, then you would simply need to set the Filename property of StartInfo to the name, if in the PATH, or the full path of the application and the Arguments property to the path of the file.如果您尝试启动的应用程序可以做到这一点,那么您只需将StartInfoFilename属性设置为名称(如果在 PATH 中)或应用程序的完整路径,并将Arguments属性设置为文件的路径。

var process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "C:\\{pathToFile}\\file.txt";
process.Start();

The above code would launch notepad opening file.txt.上面的代码将启动记事本打开 file.txt。 You can simply replace the FileName and Arguments with variables containing the paths to the application and file.您可以简单地将FileNameArguments替换为包含应用程序和文件路径的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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