简体   繁体   English

从C#调用Windows .exe

[英]Calling a Windows .exe from C#

I am trying to call tlbExp.exe from C# using Process.Start . 我试图使用Process.Start从C#调用tlbExp.exe I pass the command string as argument, but no matter what flavor of it, I always end up with an error message: 我将命令字符串作为参数传递,但无论它的含义如何,我总是会收到一条错误消息:

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)

If I try to run the command string separately in a command window while debugging, it does what it supposed to happen (tlb generated from a dll). 如果我在调试时尝试在命令窗口中单独运行命令字符串,它会执行它应该发生的事情(从dll生成tlb)。 However, I can't get it to work from the code. 但是,我无法从代码中获得它。

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); 

You need to use the overload that accepts a ProcessStartInfo object: 您需要使用接受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);

To make it generic, change the first line to this: 要使其通用,请将第一行更改为:

var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var programPath = string.Format("\"{0}\"", Path.Combine(programFiles, @"Microsoft SDKs\Windows\v6.0A\bin\tlbExp.exe"));

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

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