http://hi.baidu.com/friskyang/item/67c7dca21bc70cde5af19130打开winform进程
我这个是一个winform 调用的另一个winform程序 传递的时候这样写
参数 1 2之间用空格分开
System.Diagnostics.Process.Start(Application.StartupPath + "\\test.exe ", "参数1 参数2");
我们就是要实现这种功能!这样的程序可以在任何一个项目中使用,
通过System.Diagnostics.Process.Start("你的程序.exe 参数1")。
一般情况下,每个项目都有一个Main函数,它是整个程序的入口点,那么参数也肯定在这里被传递!
默认的Main函数:
/// <summary> /// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
我们只需改成这样:
/// <summary> /// 应用程序的主入口点。
/// </summary> [STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
Application.Run(new Form1());
else
Application.Run(new Form1(args));
}
Form1窗体的构造:
string[] args=null;
public Form1()
{
InitializeComponent();
}
public Form1(string[] args)
{
InitializeComponent();
this.args = args;
}
真的是很简单很简单的几行代码。。。我们有了args参数,剩下的代码。。。看你的需求咯。
http://blog.csdn.net/yefengnidie/article/details/6701794打开控制台进程
- //声明一个程序信息类
- System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
- //设置外部程序名
- Info.FileName = "notepad.exe";
- //设置隐藏窗口
- Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- //设置外部程序的启动参数(命令行参数)为
- test.txt Info.Arguments = "test.txt";
- //设置外部程序工作目录为 C:/
- Info.WorkingDirectory = "C://";
- //声明一个程序类
- System.Diagnostics.Process Proc;
- try { // //启动外部程序 //
- Proc = System.Diagnostics.Process.Start(Info);
- } catch (System.ComponentModel.Win32Exception exc)
- { Console.WriteLine("系统找不到指定的程序文件。/r{0}", exc); return; }
- ( ImageView_Fullscreen ) problem:
- // First create a ProcessStartInfo object.
- // First parameter the rundll32.exe command.
- // Second parameter the shimgvw.dll command along with the file name.
- System.Diagnostics.ProcessStartInfo f = new
- System.Diagnostics.ProcessStartInfo
- ("C://windows//system32//rundll32.exe",
- "C://windows//system32//shimgvw.dll,ImageView_Fullscreen " +
- fileName.TrimEnd (null));
- try
- {
- // Pass the ProcessStartInfo object to the Start function.
- System.Diagnostics.Process.Start (f);
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine (ex.ToString ());
- }
http://blog.163.com/[email protected]/blog/static/371405262009101891956334/
代码实例: 1. 打开文件 private void btOpenFile_Click(object sender, EventArgs e) { //定义一个ProcessStartInfo实例 System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); //设置启动进程的初始目录 info.WorkingDirectory = Application.StartupPath; //设置启动进程的应用程序或文档名 info.FileName = @"test.txt"; //设置启动进程的参数 info.Arguments = ""; //启动由包含进程启动信息的进程资源 try { System.Diagnostics.Process.Start(info); } catch (System.ComponentModel.Win32Exception we) { MessageBox.Show(this, we.Message); return; } } 2. 打开浏览器 private void btOpenIE_Click(object sender, EventArgs e) { //启动IE进程 System.Diagnostics.Process.Start("IExplore.exe"); } 3. 打开指定URL 方法一: private void btOpenURL_Click(object sender, EventArgs e) { //启动带参数的IE进程 System.Diagnostics.Process.Start("IExplore.exe", "http://hi.baidu.com/qinzhiyang"); } 方法二: private void btOpenURLwithArgs_Click(object sender, EventArgs e) { //定义一个ProcessStartInfo实例 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("IExplore.exe"); //设置进程参数 startInfo.Arguments = " http://hi.baidu.com/qinzhiyang "; //并且使进程界面最小化 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; //启动进程 System.Diagnostics.Process.Start(startInfo); } 4. 打开文件夹 private void btOpenFolder_Click(object sender, EventArgs e) { //获取“收藏夹”文件路径 string myFavoritesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites); //启动进程 System.Diagnostics.Process.Start(myFavoritesPath); } 5. 打印文件 private void PrintDoc() { //定义一个进程实例 System.Diagnostics.Process myProcess = new System.Diagnostics.Process(); try { //设置进程的参数 string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); myProcess.StartInfo.FileName = myDocumentsPath + "\\TxtForTest.txt"; myProcess.StartInfo.Verb = "Print"; //显示txt文件的所有谓词 foreach (string v in myProcess.StartInfo.Verbs) MessageBox.Show(v); myProcess.StartInfo.CreateNoWindow = true; //启动进程 myProcess.Start(); } catch (Win32Exception e) { if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND) { MessageBox.Show(e.Message + " Check the path." + myProcess.StartInfo.FileName); } else if (e.NativeErrorCode == ERROR_ACCESS_DENIED) { MessageBox.Show(e.Message + " You do not have permission to print this file."); } } } |