C#调用curl命令,C#无法获取curl输出解决方法

C#通过进程调用curl命令,可以形成成功,但是curl输出C#应用无法获取解决,导致curl执行成功与否无法知道。

解决方法:

可以C#调用批处理,批处理再调用curl,curl加上参数–stderr,将输出定向到文件,在通过 type命令将输出文件显示,C#就可以获取curl输出结果。

C#代码:

private int ExeUpLoadFile(TextBox resulttxtbox)
{
int ret = 0;
string workingFolder = System.IO.Directory.GetCurrentDirectory();
string curlcmd = workingFolder + “\plugin\curl” + “upload.bat”;
string cmdarg = this.UserNameTextBox.Text + " " + this.PasswordTextBox.Text + " " +
this.DeviceIPTextBox.Text + " " + this.UpdateFileTextBox.Text;
//创建进程对象
Process tmpprocess = new Process();
tmpprocess.StartInfo.FileName = curlcmd;//设定需要执行的命令
tmpprocess.StartInfo.Arguments = cmdarg;
tmpprocess.StartInfo.UseShellExecute = false;//不使用系统外壳程序启动
tmpprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
tmpprocess.StartInfo.RedirectStandardInput = true;//不重定向输入
tmpprocess.StartInfo.RedirectStandardOutput = true; //不重定向输出
tmpprocess.StartInfo.RedirectStandardError = false;
tmpprocess.StartInfo.CreateNoWindow = true;//不创建窗口

        resulttxtbox.AppendText("  开始上传升级包...\r\n");

//开始进程
if (tmpprocess.Start())
{
string log = tmpprocess.StandardOutput.ReadLine();
while (log != null)
{
if (resulttxtbox != null)
{
resulttxtbox.AppendText(log + “\r\n”);
}

if (log.IndexOf(“curl:”) > -1)
{
ret = -1;
}
log = tmpprocess.StandardOutput.ReadLine();
}
tmpprocess.Close();
tmpprocess = null;
}

if (ret == 0)
{
resulttxtbox.AppendText(" 上传升级包完成.\r\n");
}
else
{
resulttxtbox.AppendText(" 上传升级包失败.\r\n");
}

        return ret;
    }

对应批处理:

@echo off
rem *************************************************
rem
rem 上传文件
rem
rem *************************************************
rem 当前工作路径
set workdir=%~dp0

rem 获取网关登录用户
set sgwuser=%1

rem 获取网关登录密码
set sgwpasswd=%2

rem 获取网关地址
set sgwaddr=%3

rem 上传开发包
%workdir%curl.exe -u %sgwuser%:%sgwpasswd% -T %4 ftp://%sgwaddr%/update.tar.gz --stderr %workdir%result.txt
type %workdir%result.txt
del /F/Q %workdir%result.txt

友情链接:

[1]https://my.oschina.net/schuangye

[2]https://gitee.com/schuangye

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

匠心码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值