我有一个使用 Microsoft 提供的教程设置为 SingleInstance 的 C# windows 窗体,该教程在此处找到:Windows 窗体单实例应用程序 (CSWinFormSingleInstanceApp)。我遇到的问题是获取在第一个参数之后添加的任何其他参数。
程序.cs:
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceAppStarter.Start((args.Length == 0
? new frm_remPhotoViewer(string.Empty)
: new frm_remPhotoViewer(args[0])),
StartNewInstance);
}
static void StartNewInstance(object sender, StartupNextInstanceEventArgs e)
{
FormCollection forms = Application.OpenForms;
//MessageBox.Show(e.CommandLine[1]);
if (forms["frm_remPhotoViewer"] != null
&& forms["frm_remPhotoViewer"].WindowState == FormWindowState.Minimized)
{
forms["frm_remPhotoViewer"].WindowState = FormWindowState.Normal;
forms["frm_remPhotoViewer"].Activate();
}
else if (forms["frm_remPhotoViewer"] == null)
{
frm_remPhotoViewer f = new frm_remPhotoViewer("HELP ME!!!");
f.ShowDialog();
}
}
SingleInstanceAppStarter.cs:
static SingleInstanceApp app = null;
// Construct SingleInstanceApp object, and invoke its run method.
public static void Start(Form f, StartupNextInstanceEventHandler handler)
{
if (app == null && f != null)
app = new SingleInstanceApp(f);
// Wire up StartupNextInstance event handler.
app.StartupNextInstance += handler;
app.Run(Environment.GetCommandLineArgs());
}
SingleInstanceAppHelper.cs:
public SingleInstanceApp()
{
}
public SingleInstanceApp(Form f)
{
// Set IsSingleInstance property to true to make the application
base.IsSingleInstance = true;
// Set MainForm of the application.
this.MainForm = f;
}
被注释掉的行是我要添加到活动表单的文本。