C#Process执行批处理后如何获取返回值?

本文介绍如何在C#中使用System.Diagnostics.Process类执行批处理文件并获取返回值。通过设置ProcessStartInfo对象,启动批处理并捕获输出流,从而判断批处理运行期间是否出现错误。示例代码和批处理内容包含删除网络连接和建立网络共享的命令。
摘要由CSDN通过智能技术生成

代码如下   
p.StartInfo = new System.Diagnostics.ProcessStartInfo(path, pwd);
 p.Start();
其中path是个BAT的路径!
我想要得到执行后的返回值来判断批处理运行期间是否错误?
请问如何做呢?
批处理程序内容如下:
@echo off
for /f "delims=" %%a in (PCList.config) do net use \\%%a\ipc$ /delete
for /f "delims=" %%a in (PCList.config) do net use \\%%a\ipc$ %1 /user:administrator
pause

 

// Define the namespaces used by this sample.
using System;
using System.Text;
using System.Globalization;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
namespace ProcessAsyncStreamSamples
{
    class ProcessNetStreamRedirection
    {
        // Define static variables shared by class methods.
        private static StreamWriter streamError =null;
        private static String netErrorFile = "";
        private static StringBuilder netOutput = null;
        private static bool errorRedirect = false;
        private static bool errorsWritten = false;
        public static void RedirectNetCommandStreams()
        {
            String netArguments;
            Process netProcess;
            // Get the input computer name.
            Console.WriteLine("Enter the computer name for the net view command:");
            netArguments = Console.ReadLine().ToUpper(CultureInfo.InvariantCulture);
            if (String.IsNullOrEmpty(netArguments))
            {
                // Default to the help command if there is not an input argument.
                netArguments = "/?";
            }
            // Check if errors should be redirected to a file.
            errorsWritten = false;
            Console.WriteLine("Enter a fully qualified path to an error log file");
            Console.WriteLine("  or just press Enter to write errors to console:");
            netErrorFile = Console.ReadLine().ToUpper(CultureInfo.InvariantCulture);
         
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值