C# 运行指定程序,可以方便扩展
2012-05-14 19:35
309 查看
主要测试代码:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; using System.Diagnostics; namespace CallBat { /// <summary> /// 作者:Jave.Lin /// </summary> static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); if (args.Length == 0) { Console.WriteLine("args.Length==0 error!"); return; } string fileName = args[0]; if (!File.Exists(fileName)) { Console.WriteLine("fileName is not Exists error! the fileName is:\r\n"+fileName); return; } string fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName); Process[] tps = Process.GetProcessesByName(fileNameNoExtension); if (tps != null) { foreach (Process tp in tps) { if (tp.ProcessName == fileNameNoExtension) { try { Console.WriteLine("[" + fileNameNoExtension + "] process had run, and then will kill it!"); tp.Kill(); tp.Close(); } catch { } } } } string workDirectory = Path.GetDirectoryName(fileName); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true;//设置为false将会看到程序窗口 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//启动进程时窗口状态 p.StartInfo.RedirectStandardOutput = true; //p.StartInfo.FileName = Server.MapPath("a.bat"); p.StartInfo.FileName = fileName;//如果a.bat在System32文件夹中,此处只需填写文件名即可 p.StartInfo.WorkingDirectory = workDirectory; if (args.Length > 1) { string argsStr = string.Empty; for (int i = 1; i < args.Length; i++) { argsStr += args[i] + " "; } p.StartInfo.Arguments = argsStr; } p.Start(); p.Close(); } } }
相关文章推荐
- 写程序 控制cpu占用率-4 绘制CPU使用率的正弦曲线2 通过C#指定线程运行的 CPU
- C#程序Debug文件夹可以运行,无法调试
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 改注册表,在一定时间内只能运行指定程序,知道用组策略管理器,可以实现,不过我想用编程的方法。
- at 命令来安排命令、脚本或程序在指定的日期和时间运行。您也可以使用此命令查看现有的计划任务。
- C# Winform程序如何获取运行路径, 控制台也可以
- C# 程序运行无法加载 C++ DLL“xxx.dll”: 找不到指定的模块
- C#检测程序重复运行的函数(可以在多用户登录情况下检测)
- C# Webservice 解决在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度问
- 编写完之后如何调试运行 用Microsoft Visual C++ 6.0可以编写C#程序吗
- C# 编译器选项 /platform(指定输出平台)32位程序运行到x64平台的问题
- C# 编译器选项 /platform(指定输出平台)32位程序运行到x64平台的问题
- C#程序单一运行
- c# 仿照计划任务(定时提示、定时运行程序、定时打开url(前台/后台))/每天/每周/每月/一次 多时间段
- 开发可以自动运行程序的U盘
- 简单代码实现C#中运行另外一个程序
- C# 做一个指定概率的抽奖程序
- C#启用管理员权限运行程序
- C# 测试程序运行时间
- 编写一个程序,可以通过次程序完成一个表的创建操作,输入表的名称,各个列的名称及类型,输入完成后直接通过JDBC创建指定的表。