Loading... ### 主类 ```c# using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace hyjk.Commons { public class Class_cUrl { private string args = ""; /// <summary> /// 参数 /// </summary> public string Args { get { return args; } set { args = value; } } /// <summary> /// 设置参数 /// </summary> /// <param name="value">参数值</param> public void SetArg(string value) { if (string.IsNullOrEmpty(args)) { args = value; } else { args += " " + value; } } ProcessStartInfo processStartInfo = new ProcessStartInfo(); Process process = new Process(); //将cmd的标准输入和输出全部重定向到.NET的程序里 #region 运行 /// <summary> /// 运行 /// </summary> /// <returns></returns> public string Run() { processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardInput = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardError = false; processStartInfo.CreateNoWindow = true; processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; processStartInfo.FileName = @"curl.exe"; processStartInfo.Arguments = args; try { process = Process.Start(processStartInfo); } catch { return process.StandardError.ReadToEnd(); } string resaultValue = process.StandardOutput.ReadToEnd(); process.WaitForExit(); process.Close(); return resaultValue; } #endregion 运行 } } ``` ### 调用 ``` public static TcURL(){ Class_cUrl c = new Class_cUrl(); string arg = "-XPOST -T body.txt " + url + " -k --output ret.txt -s --show-error"; c.SetArg(arg); c.Run(); } ``` 最后修改:2020 年 09 月 09 日 © 允许规范转载 打赏 赞赏作者 赞 如果觉得我的文章对你有用,请随意赞赏