【问题标题】:c# wanting to pipeline the output from atmel studio command line to command promptc# 想要将 atmel studio 命令行的输出通过管道传输到命令提示符
【发布时间】:2019-12-26 05:29:47
【问题描述】:

我想使用命令提示符自动刷新硬件。我检查了从 atprogram.exe 传递到命令提示符的字符串是否正确,现在已将其缩小到我的输出

 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.IO;

 namespace AutoFlash
 {
 class Program
 {
    public static void Main(string[] args)
    {

        string atProgramLocaction = "\"C:\\Program Files 
        (x86)\\Atmel\\Studio\\7.0\\atbackend\\atprogram.exe\"";
        string atProgramArgs = "-t atmelice -i swd -d DEVICENAME program 
        -f";
        string fileLocation = "C\:\FILE.HEX";

        string command = atProgramLocaction + " " + atProgramArgs + " " + 
        fileLocation;

        Process AtmelCommand = new Process();
        AtmelCommand.StartInfo.FileName = "cmd.exe";
        AtmelCommand.StartInfo.Arguments = command;
        AtmelCommand.StartInfo.RedirectStandardInput = true;
        AtmelCommand.StartInfo.UseShellExecute = false;
        AtmelCommand.StartInfo.RedirectStandardOutput = true;
        AtmelCommand.Start();

        Console.WriteLine(AtmelCommand.StandardOutput.ReadToEnd());
        AtmelCommand.WaitForExit();

    }
  }
}

理想情况下希望看到 atmel 命令行输出“Firmware check OK 编程成功完成”传递给 cmd.exe 并打印以向用户确认固件已成功刷新。当前发生的是弹出一个空白命令行窗口。任何帮助都将不胜感激!

【问题讨论】:

    标签: c# command-line console-application pipeline atmelstudio


    【解决方案1】:

    一些事情;输出可能来自StandardError 而不是StandardOut,另外,我认为您需要在WaitForExit 之后读取流。

    试试这样的:-

    var psi = new ProcessStartInfo();
    
    psi.FileName = "program.exe";
    psi.Arguments = "-v";
    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    
    var p = Process.Start(psi);
    
    var stdout = string.Empty;
    var stderr = string.Empty;
    
    p.ErrorDataReceived += (sender, e) =>
    {
        stderr += e.Data;
    };
    
    p.BeginErrorReadLine();
    
    string line;
    
    while ((line = p.StandardOutput.ReadLine()) != null)
    {
        stdout += line;
    }
    
    p.WaitForExit();
    
    Console.WriteLine(stdout);
    Console.WriteLine(stderr);
    Console.WriteLine(p.ExitCode);
    

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 2019-08-17
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2013-10-13
      相关资源
      最近更新 更多