1

我使用Process调用一个批处理cmd,然后读取执行的输出。通过输出行,我分析执行的成功或失败。在win7中一切正常,但在WindowsXP中,我无法从标准输出中读取任何行。我保证批处理文件可以在 WindowsXP 命令控制台窗口中手动运行。以下是我的代码:

//this method is entrance of execution
public bool buildApk()
{
    string launch = "\"" + System.Environment.CurrentDirectory + @"\SmartCon\start_up_build" + "\"";
    string output = ExecuteAsync(launch);
    return output.Contains("SUCCESS");
}

public static string ExecuteAsync(string dosCommand)
{
    string output = "";     
    StringBuilder sbOutput=new StringBuilder ("");

    if (dosCommand != null && dosCommand != "")
    {
        Process process = new Process();     
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe";      
        startInfo.Arguments = "/C " + dosCommand;   
        startInfo.UseShellExecute = false;    
        startInfo.RedirectStandardInput = false;   
        startInfo.RedirectStandardOutput = true;   
        startInfo.CreateNoWindow = true;     
        process.StartInfo = startInfo;

        try
        {
            if (process.Start())       
            {
                while (!process.HasExited )
                {
                    string d = process.StandardOutput.ReadLine();//here I read nothing in windowxp, but in win7, all things go well
                    sbOutput.Append(d);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error:" + ex.Message + "\n" + ex.StackTrace);
        }
        finally
        {
            if (process != null)
            {
                process.Close();
                process.Dispose();
                process = null;
            }
        }
    }
    return sbOutput .ToString ();
}
4

0 回答 0