using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace callBat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
callBat(textBox1.Text);
}
static void callBat(string arg)
{
Process proc = null;
try
{/*
如果要运行时隐藏dos窗口,需使用下面的代码
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
*/
proc = new Process();
//proc.StartInfo.FileName = @"D:\mybatch.bat";
proc.StartInfo.FileName = @"mybatch.bat";
proc.StartInfo.Arguments = arg;//this is argument
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.UseShellExecute = false;
//proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
}
}
}
}
C#: 执行批处理文件(*.bat)的方法
本文介绍一下在C#中执行批处理文件(*.bat)的方法。
实现代码如下:
static void Main(string[] args)
{
Process proc = null;
try
{
proc = new Process();
proc.StartInfo.FileName = @"D:\adapters\setup\mybatch.bat";
proc.StartInfo.Arguments = string.Format("10");//this is argument
proc.StartInfo.CreateNoWindow = false;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString());
}
}
如果要运行时隐藏dos窗口,需使用下面的代码
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;