Calling a Windows .exe from C#



我正在尝试使用 Process.Start 从 C# 调用tlbExp.exe。我将命令字符串作为参数传递,但无论它是什么风格,我总是以错误消息结束:

The system cannot find the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)

如果我尝试在调试时在命令窗口中单独运行命令字符串,它会执行它应该发生的事情(从 dll 生成的 tlb)。但是,我无法让它从代码中工作。

string tlb;
...
tlb += @"C:Program filesMicrosoft SDKsWindowsv6.0AbintlbExp.exe";
tlb += @""""; tlb += @" """; tlb += outputDllPath;
tlb += @""" /out:"""; tlb += outputTlbPath; tlb += @"""";
Process.Start(tlb); 

您需要使用接受ProcessStartInfo对象的重载:

var programPath = @"""C:Program filesMicrosoft SDKsWindowsv6.0AbintlbExp.exe""";
var info = new ProcessStartInfo(programPath);
info.Arguments = string.Format(""{0}" /out:"{1}"", outputDllPath, outputTlbPath);
Process.Start(info);

要使其通用,请将第一行更改为:

var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var programPath = string.Format(""{0}"", Path.Combine(programFiles, @"Microsoft SDKsWindowsv6.0AbintlbExp.exe"));

最新更新