我試圖將參數發送到已經在處理器中的應用程序。我正在使用Mutex來查找應用程序是否正在運行或尚未運行。我需要發送任何命令行參數,並將該文本添加到列表框中。但參數正在進入,但值不會被添加到列表框。應用程序的名稱是「爲MyApplication」和增加值,以列表框的參數()發送參數到C#中的正在運行的應用程序
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 Frm1 = new Form1();
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "MYAPPLICATION", out createdNew)) //Finding if application is running or not
{
if (createdNew)
{
//foreach (string abc in Environment.GetCommandLineArgs())
//{
// MessageBox.Show(abc);
//}
Application.Run(Frm1);
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
Frm1.parameters(Environment.GetCommandLineArgs());
break;
}
}
}
}
}
可能重複:http://stackoverflow.com/questions/3793997/pass-arguments-to-running-application –