Process.start 以应用程序和文件名作为变量
Process.start with application & filename as variables
一直在寻找,但令人惊讶的是找不到这个具体问题:
我想使用 C#(通过单击表单中的按钮)运行 特定文件和特定应用程序。
当使用“Process.start(变量)”时,我只能选择两者之一。
并且通过使用“Process.startinfo.filename”(如:https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.filename?view=net-5.0)似乎也是如此。
难道不能以某种“简单”的方式将两者结合起来吗?
谢谢。
通常情况下,您会 运行 一个包含使用命令参数(即 'notepad.exe file.txt')的应用程序的文件。
如果您尝试启动的应用程序可以做到这一点,那么您只需将 StartInfo
的 Filename
属性 设置为名称,如果在 PATH 中,或应用程序的完整路径和 Arguments
属性 到文件路径。
var process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "C:\{pathToFile}\file.txt";
process.Start();
以上代码将启动记事本打开 file.txt。您可以简单地将 FileName
和 Arguments
替换为包含应用程序和文件路径的变量。
一直在寻找,但令人惊讶的是找不到这个具体问题:
我想使用 C#(通过单击表单中的按钮)运行 特定文件和特定应用程序。
当使用“Process.start(变量)”时,我只能选择两者之一。
并且通过使用“Process.startinfo.filename”(如:https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.filename?view=net-5.0)似乎也是如此。
难道不能以某种“简单”的方式将两者结合起来吗?
谢谢。
通常情况下,您会 运行 一个包含使用命令参数(即 'notepad.exe file.txt')的应用程序的文件。
如果您尝试启动的应用程序可以做到这一点,那么您只需将 StartInfo
的 Filename
属性 设置为名称,如果在 PATH 中,或应用程序的完整路径和 Arguments
属性 到文件路径。
var process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "C:\{pathToFile}\file.txt";
process.Start();
以上代码将启动记事本打开 file.txt。您可以简单地将 FileName
和 Arguments
替换为包含应用程序和文件路径的变量。