第一個程序:
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WindowsFormsApplication1.exe"; //啟動的應用程序名稱
startInfo.Arguments = "我是由控制台程序傳過來的參數,如果傳多個參數用空格隔開"+" 第二個參數";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo);
}
catch (Exception ex)
{
throw;
}
第二個程序:
需要改Main方法
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
public 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(string[] args)
{
InitializeComponent();
//this.ShowIcon = false;
this.ShowInTaskbar = false; //隱藏在系統任務欄
this.WindowState = FormWindowState.Minimized;
//this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
this.args = args;
}
如此,利用傳過來的參數就可以在第二個程序里執行了
