请教如何在c#程序中调用.exe和.bat的文件 我现在写一个小程序要分别调用到一个.exe文件一个.bat批处理文件,让他们分别执行先,然后调用oracle的存储过程,可是不知道在c#里面如何调用前两种,请指教。多谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Diagnostics.Process proc=System.Diagnostics.Process.Start(@"c:\1.exe");proc.WaitForExit();proc=System.Diagnostics.Process.Start(@"c:\2.bat");proc.WaitForExit(); System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "mt3000.exe"; //程序名称 proc.StartInfo.Arguments = "/p1 /b38400 /fstock /mr"; //参数 proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //隐藏 proc.Start(); //执行 .bat批处理文件,楼上的已经说的很清楚了,如果要显示出执行控制台程序时生成的信息,查找一下Pipe, System.Diagnostics.Process.Start("iexplorer.exe","http://www.sina.com.cn");相当于命令行 using System;using System.Diagnostics;using System.Text;namespace ZZ{ /// <summary> /// CmdUtility 的摘要说明。 /// </summary> public class CmdUtility { /// <summary> /// 执行cmd.exe命令 /// </summary> /// <param name="commandText">命令文本</param> /// <returns>命令输出文本</returns> public static string ExeCommand(string commandText) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string strOutput = null; try { p.Start(); p.StandardInput.WriteLine(commandText); p.StandardInput.WriteLine("exit"); strOutput = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); } catch(Exception e) { strOutput = e.Message; } return strOutput; } /// <summary> /// 执行多条cmd.exe命令 /// </summary> /// <param name="commandArray">命令文本数组</param> /// <returns>命令输出文本</returns> public static string ExeCommand(string [] commandTexts) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string strOutput = null; try { p.Start(); foreach(string item in commandTexts) { p.StandardInput.WriteLine(item); } p.StandardInput.WriteLine("exit"); strOutput = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); } catch(Exception e) { strOutput = e.Message; } return strOutput; } /// <summary> /// 启动外部Windows应用程序,隐藏程序界面 /// </summary> /// <param name="appName">应用程序路径名称</param> /// <returns>true表示成功,false表示失败</returns> public static bool StartApp(string appName) { return StartApp(appName,ProcessWindowStyle.Hidden); } /// <summary> /// 启动外部应用程序 /// </summary> /// <param name="appName">应用程序路径名称</param> /// <param name="style">进程窗口模式</param> /// <returns>true表示成功,false表示失败</returns> public static bool StartApp(string appName,ProcessWindowStyle style) { bool blnRst = false; Process p = new Process(); p.StartInfo.FileName = appName;//exe,bat and so on p.StartInfo.WindowStyle = style; try { p.Start(); p.WaitForExit(); p.Close(); //while (!p.HasExited) //{ // Thread.Sleep(1000); //} blnRst = true; } catch { } return blnRst; } /// <summary> /// 启动外部应用程序,隐藏程序界面 /// </summary> /// <param name="appName">应用程序路径名称</param> /// <param name="arguments">启动参数</param> /// <returns>true表示成功,false表示失败</returns> public static bool StartApp(string appName,string arguments) { return StartApp(appName,arguments,ProcessWindowStyle.Hidden); } /// <summary> /// 启动外部应用程序 /// </summary> /// <param name="appName">应用程序路径名称</param> /// <param name="arguments">启动参数</param> /// <param name="style">进程窗口模式</param> /// <returns>true表示成功,false表示失败</returns> public static bool StartApp(string appName,string arguments,ProcessWindowStyle style) { bool blnRst = false; Process p = new Process(); p.StartInfo.FileName = appName;//exe,bat and so on p.StartInfo.WindowStyle = style; p.StartInfo.Arguments = arguments; try { p.Start(); p.WaitForExit(); p.Close(); blnRst = true; } catch { } return blnRst; } }} 静态方法这样写为什么不对 HttpWebRequest Post 数据分包问题,如果才能整包发送数据? select 怎样返回 dbnull.value 求windos文本编辑器里加一段“查找”的选项代码 iframe的链接问题 各位同仁有谁知道吗?先谢了!! 如何用DataGrid来处理多表问题? vs2010 winform 程序开发 怎么在richtextbox控件中设置二级标题 在C#中的重载是不是只有在基类方法是virtual的情况下才行? c#异常 集合已修改;可能无法执行枚举操作 如何给文件文件定位取值行数 关于动态加载多个带参数用户自定义控件问题,新人第一次提问题多多支持啦!
proc.WaitForExit();proc=System.Diagnostics.Process.Start(@"c:\2.bat");
proc.WaitForExit();
proc.StartInfo.FileName = "mt3000.exe"; //程序名称
proc.StartInfo.Arguments = "/p1 /b38400 /fstock /mr"; //参数
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //隐藏
proc.Start(); //执行
using System.Diagnostics;
using System.Text;
namespace ZZ
{
/// <summary>
/// CmdUtility 的摘要说明。
/// </summary>
public class CmdUtility
{
/// <summary>
/// 执行cmd.exe命令
/// </summary>
/// <param name="commandText">命令文本</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
/// <summary>
/// 执行多条cmd.exe命令
/// </summary>
/// <param name="commandArray">命令文本数组</param>
/// <returns>命令输出文本</returns>
public static string ExeCommand(string [] commandTexts)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
foreach(string item in commandTexts)
{
p.StandardInput.WriteLine(item);
}
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
/// <summary>
/// 启动外部Windows应用程序,隐藏程序界面
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName)
{
return StartApp(appName,ProcessWindowStyle.Hidden);
}
/// <summary>
/// 启动外部应用程序
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <param name="style">进程窗口模式</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName,ProcessWindowStyle style)
{
bool blnRst = false;
Process p = new Process();
p.StartInfo.FileName = appName;//exe,bat and so on
p.StartInfo.WindowStyle = style;
try
{
p.Start();
p.WaitForExit();
p.Close();
//while (!p.HasExited)
//{
// Thread.Sleep(1000);
//}
blnRst = true;
}
catch
{
}
return blnRst;
}
/// <summary>
/// 启动外部应用程序,隐藏程序界面
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <param name="arguments">启动参数</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName,string arguments)
{
return StartApp(appName,arguments,ProcessWindowStyle.Hidden);
}
/// <summary>
/// 启动外部应用程序
/// </summary>
/// <param name="appName">应用程序路径名称</param>
/// <param name="arguments">启动参数</param>
/// <param name="style">进程窗口模式</param>
/// <returns>true表示成功,false表示失败</returns>
public static bool StartApp(string appName,string arguments,ProcessWindowStyle style)
{
bool blnRst = false;
Process p = new Process();
p.StartInfo.FileName = appName;//exe,bat and so on
p.StartInfo.WindowStyle = style;
p.StartInfo.Arguments = arguments;
try
{
p.Start();
p.WaitForExit();
p.Close();
blnRst = true;
}
catch
{
}
return blnRst;
}
}
}