C# 批量处理文件好处
2010-12-14 17:15 三皮开发时 阅读(656) 评论(0) 编辑 收藏 举报在接触的文件中,后缀名为.bat 批处理文件我们并不陌生,顾名思义,它的好处就是批处理。
我接触的生活场景,1)例如在PDA的开发中,要在PDA上安装一些软件,写好批处理命令,批安装一些应用程序。
2)平板电脑开发中,要求程序启动后,开启TOMCAT服务,开启数据库服务(MySQL)
开启MySQL服务,需启动MySQL bin目录下的mysqld.exe,服务启动才能查阅数据库数据,平板电脑上安装的是绿色版MySQL,程序运行首先要运行MySQL服务
贴段代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//8080没打开需要打开
if (!MySQLCmd.IsOpenPort(8080))
{
MySQLCmd.StartCmd(CustomConfig.TomCatSetAddr, "tomcat_start.bat");
}
//MySQL 端口3306
if (!MySQLCmd.IsOpenPort(3306))
{
MySQLCmd.StartCmd(CustomConfig.TomCatSetAddr, "mysql_start.bat");
}
FrmLogin fLogin = (FrmLogin)FormUtility.GetForm(typeof(FrmLogin));
Application.Run(fLogin);
if (!MySQLCmd.IsOpenPort(8080))
{
MySQLCmd.StartCmd(CustomConfig.TomCatSetAddr, "tomcat_start.bat");
}
//MySQL 端口3306
if (!MySQLCmd.IsOpenPort(3306))
{
MySQLCmd.StartCmd(CustomConfig.TomCatSetAddr, "mysql_start.bat");
}
FrmLogin fLogin = (FrmLogin)FormUtility.GetForm(typeof(FrmLogin));
Application.Run(fLogin);
public class MySQLCmd
{
/// <summary>
/// 执行Cmd命令
/// </summary>
/// <param name="workingDirectory">要启动的进程的目录</param>
/// <param name="command">要执行的命令</param>
public static void StartCmd(String workingDirectory, String command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = workingDirectory;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
}
/// <summary>
/// 是否网络正常
/// </summary>
/// <param name="strip">主机地址</param>
/// <returns></returns>
public static bool PingHost(string strip)
{
Ping myping = new Ping();
PingOptions myoptions = new PingOptions();
myoptions.DontFragment = true;//允许数据分段,还有一个ttl选项
//创建一个32b的缓冲区
string data = "abcdefghijklmnopqrstuvwxy123456";
byte[] buff = Encoding.ASCII.GetBytes(data);
PingReply mypingreply = myping.Send(strip, 500, buff, myoptions);
if (mypingreply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 是否端口已被打开
/// </summary>
/// <param name="port">端口号</param>
/// <returns></returns>
public static bool IsOpenPort(int port)
{
bool tcpListen = false; //TCP
bool udpListen = false;//设定端口状态标识位 UDP 暂没写
System.Net.IPAddress myIpAddress = IPAddress.Parse("127.0.0.1");
System.Net.IPEndPoint myIpEndPoint = new IPEndPoint(myIpAddress, port);
try
{
System.Net.Sockets.TcpClient tcpClient = new TcpClient();
tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
tcpListen = true;
return tcpListen;
}
catch
{
return false;
}
}
}
{
/// <summary>
/// 执行Cmd命令
/// </summary>
/// <param name="workingDirectory">要启动的进程的目录</param>
/// <param name="command">要执行的命令</param>
public static void StartCmd(String workingDirectory, String command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = workingDirectory;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
}
/// <summary>
/// 是否网络正常
/// </summary>
/// <param name="strip">主机地址</param>
/// <returns></returns>
public static bool PingHost(string strip)
{
Ping myping = new Ping();
PingOptions myoptions = new PingOptions();
myoptions.DontFragment = true;//允许数据分段,还有一个ttl选项
//创建一个32b的缓冲区
string data = "abcdefghijklmnopqrstuvwxy123456";
byte[] buff = Encoding.ASCII.GetBytes(data);
PingReply mypingreply = myping.Send(strip, 500, buff, myoptions);
if (mypingreply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 是否端口已被打开
/// </summary>
/// <param name="port">端口号</param>
/// <returns></returns>
public static bool IsOpenPort(int port)
{
bool tcpListen = false; //TCP
bool udpListen = false;//设定端口状态标识位 UDP 暂没写
System.Net.IPAddress myIpAddress = IPAddress.Parse("127.0.0.1");
System.Net.IPEndPoint myIpEndPoint = new IPEndPoint(myIpAddress, port);
try
{
System.Net.Sockets.TcpClient tcpClient = new TcpClient();
tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
tcpListen = true;
return tcpListen;
}
catch
{
return false;
}
}
}
判断端口是否打开,未打开则执行.bat程序
tomcat_start.bat 里的内容是:mysqld.exe --console
OK.