简体   繁体   中英

Calling a Windows .exe from C#

I am trying to call tlbExp.exe from C# using Process.Start . 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). 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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