gpt4 book ai didi

c# - 在 C# 中执行批处理文件

转载 作者:行者123 更新时间:2023-11-30 13:41:44 25 4
gpt4 key购买 nike

我有一个批处理文件,基本上是从站点下载文件或返回一条错误消息,指出文件不可用。我想在 C#(SSIS 脚本组件)中执行此 .bat 文件并将文件名保存到变量(如果文件已下载)或将错误消息分配给变量。

提前致谢

最佳答案

使用 Process.Start 执行批处理文件.您可以通过读取返回的 Process 对象上的 StandardError 和 StandardOutput 流来获取文本形式的流程输出。您需要将 RedirectStandardOutput 属性设置为 true。

这是来自 MSDN 的示例:

 Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "yourbatchfile.bat";
p.Start();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

关于c# - 在 C# 中执行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com