从C#调用Windows .exe

时间:2012-12-10 11:36:46

标签: c#

我正在尝试使用tlbExp.exe从C#中调用Process.Start。我将命令字符串作为参数传递,但无论它的含义如何,我总是会收到一条错误消息:

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 files\Microsoft SDKs\Windows\v6.0A\bin\tlbExp.exe";
tlb += @""""; tlb += @" """; tlb += outputDllPath;
tlb += @""" /out:"""; tlb += outputTlbPath; tlb += @"""";
Process.Start(tlb); 

1 个答案:

答案 0 :(得分:2)

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

var programPath = @"""C:\Program files\Microsoft SDKs\Windows\v6.0A\bin\tlbExp.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 SDKs\Windows\v6.0A\bin\tlbExp.exe"));