How to send arguments to a running Process in C#?

This question already has an answer here:

  • What is the correct way to create a single-instance application? 35 answers

  • I use this: https://code.msdn.microsoft.com/windowsapps/CSWinFormSingleInstanceApp-d1791628

    It's single instance, and supports command line args. My program is started like this:

    [STAThread]
    static void Main(String [] args) {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
    
        Form1 mf = new Form1(); // your form
        SingleInstanceAppStarter.Start(mf, StartNewInstance);
    }
    
    ...
    
    private static void StartNewInstance(object sender, StartupNextInstanceEventArgs e) {
        String cmdArg = e.CommandLine[1]; // yes, 1 to get the first.  not zero.
        ...
    }
    

    You'll also need this:

    class SingleInstanceAppStarter
    {
        static SingleInstanceApp app = null;
    
        public static void Start(Form f, StartupNextInstanceEventHandler handler)
        {
            if (app == null && f != null)
            {
                app = new SingleInstanceApp(f);
            }
            app.StartupNextInstance += handler;
            app.Run(Environment.GetCommandLineArgs());
        }
    }
    

    and this:

    class SingleInstanceApp : WindowsFormsApplicationBase
    {
        public SingleInstanceApp() { }
    
        public SingleInstanceApp(Form f)
        {
            base.IsSingleInstance = true;
            this.MainForm = f;
        }
    }
    

    Note that both of those classes use the Microsoft.VisualBasic.ApplicationServices assembly (You'll have to reference it).

    链接地址: http://www.djcxy.com/p/51154.html

    上一篇: 如何防止我的应用一次又一次启动

    下一篇: 如何将参数发送到C#中正在运行的进程?