【问题标题】:How to run CL.exe using Process.Start()?如何使用 Process.Start() 运行 CL.exe?
【发布时间】:2012-02-27 13:47:32
【问题描述】:

我有以下代码

using (StreamWriter outfile = new StreamWriter(@"f:\trial.cpp"))
{
    outfile.Write(txtCode.InnerText);
}

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"cl.exe", @"  'trial.cpp'");

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.UserName = "asdasd";
SecureString secureString = new SecureString();
foreach (char c in "abcded")
{
    secureString.AppendChar(c);
}
procStartInfo.Password = secureString;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = @"f:\";
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();

如何将文件名作为参数传递?上面的代码没有运行,我已经尝试了所有的完整路径,不同的路径选项。

谁能帮忙?

【问题讨论】:

  • 没有运行?错误是什么??
  • 定义“不运行”。解释确切什么正在发生以及您期望发生什么。
  • “不运行”是什么意思?进程未启动?命令行参数错误?
  • 用字符串参数交换'@"f:\trial.cpp"'?此外,在处理之前确保文件存在可以解决问题,例如if ( new System.IO.FileInfo( yourFileAndPath ).Exists )...
  • “不运行”的意思是,它不会产生任何输出。结果字符串中只有“Trial.cpp”,没有任何意义。我检查了trial.cpp所在的目录,没有putput文件,检查了cl.exe目录和c:\windows\system32,没有输出。

标签: c# process.start cl.exe


【解决方案1】:

参数设置不正确。你有:

var procStartInfo = new ProcessStartInfo(@"cl.exe", @"  'trial.cpp'");

名称中有空格和单引号的地方。试试:

var procStartInfo = new ProcessStartInfo(@"cl.exe", @"trial.cpp");

【讨论】:

  • 同样的结果。它不会有太大的区别,因为我还可以提供带引号的文件路径,额外的空间只会在参数中添加空间,这对于解析来说应该不是问题。
【解决方案2】:

编辑

startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "CL.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "trial.cpp";

try
{
    using (Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.WaitForExit();
    }
}
catch
{
    // error handling
}

这里的重点是 CL 是命令行可执行文件,而不是 Windows GUI 应用程序。

http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx

http://msdn.microsoft.com/en-us/library/kezkeayy.aspx

http://msdn.microsoft.com/en-us/library/9s7c9wdw.aspx

【讨论】:

  • @Oded 我很抱歉,但他可以用谷歌搜索并轻松找到答案。
  • 是的。而本可以这样评论的。
  • 好吧,我已经用谷歌搜索了足够多的东西,我认为在这里发帖的每个人都会很聪明,可以先用谷歌搜索,只有在找不到解决方案时才来 StackOverflow。我可以再给你 5 个链接,其中包含更多详细信息和更精确的代码,但我已经尝试了所有方法,但没有任何效果,因此寻求帮助。
  • @user1235483 - 不错的假设。有很多问题表明情况并非如此......您确实应该在您的问题中发布您尝试过的内容。
【解决方案3】:

如果 cl.exe 不在系统 PATH 中(默认情况下不在),则启动过程将找不到可执行文件,并且无法执行运行。

所以我怀疑你看到 cl.exe 不在系统路径中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多