c#执行bat批处理文件并回显执行结果

研究了好几个小时才搞定

函数体

       public static string ExcuteBatFile(string batPath, ref string errMsg)
        {
            try
            {
                if (errMsg == null) throw new ArgumentNullException("errMsg");
                string output;
                using (Process process = new Process())
                {
                    FileInfo file = new FileInfo(batPath);
                    if (file.Directory != null)
                    {
                        process.StartInfo.WorkingDirectory = file.Directory.FullName;
                    }
                    process.StartInfo.FileName = batPath;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
                    process.WaitForExit();
                    output = process.StandardOutput.ReadToEnd();
                    errMsg = process.StandardError.ReadToEnd();
                }
                return output + "|||||" + errMsg;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

调用:

 private void button2_Click(object sender, EventArgs e)
        {
            // var bat = System.Threading.Thread.GetDomain().BaseDirectory + "EZHbozmZpC.bat";
            string path = @"C:\sql0\" + textBox1.Text;
            //string path = @"C:\sql0\EZHbozmZpC.bat";
            string errMsg = string.Empty;
            string output = ExcuteBatFile(path, ref errMsg);
            textBox3.Text = output;
        }

返回值:


C:\sql0>F:

F:\>cd F:\soft\oracle\sqluldr2 

F:\soft\oracle\sqluldr2> ******** 
           0 rows exported at 2022-04-15 19:21:32, size 0 MB.
         734 rows exported at 2022-04-15 19:21:32, size 0 MB.
         output file c:\888.csv closed at 734 rows, size 0 MB.
|||||

c#生成bat文件,并且执行

 private void button2_Click(object sender, EventArgs e)
        {
            string batfile = getStr(10);
            string fileContent = "F:" + System.Environment.NewLine + "cd F:\\soft\\oracle\\sqluldr2" + System.Environment.NewLine
             + "sqluldr264 USER=***/***@*** QUERY=\"select * from " + textBox1.Text + "\"  head=yes FILE=c:\\"+ batfile + ".csv";
           
            string filePath = "c:\\sql0\\" + batfile +".bat";
            if (!File.Exists(filePath))
            {
                FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(fileContent);//开始写入值
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(fileContent);//开始写入值
                sr.Close();
                fs.Close();
            }

            // var bat = System.Threading.Thread.GetDomain().BaseDirectory + "temp.bat";
            string path = filePath;
            //string path = @"C:\sql0\EZHbozmZpC.bat";
            string errMsg = string.Empty;
            string output = ExcuteBatFile(path, ref errMsg);
            textBox3.Text = output;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值