Pass arguments to running application

前端 未结 5 1017
猫巷女王i
猫巷女王i 2020-12-01 08:20

I am making an image uploader (upload image to image hosting website) and I\'m having some issues passing an argument (image location to an already running application)

5条回答
  •  一向
    一向 (楼主)
    2020-12-01 08:59

    I have figured it out, so awesome thanks for the person who posted the http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64 link, this is exactly what I was looking for!

    Here's a the full solution:

    static class Program
    {
        [STAThread]
        static void Main(params string[] Arguments)
        {
            SingleInstanceApplication.Run(new ControlPanel(), NewInstanceHandler);
        }
    
        public static void NewInstanceHandler(object sender, StartupNextInstanceEventArgs e)
        {
            string imageLocation = e.CommandLine[1];
            MessageBox.Show(imageLocation);
            e.BringToForeground = false;
            ControlPanel.uploadImage(imageLocation);
        }
    
        public class SingleInstanceApplication : WindowsFormsApplicationBase
        {
            private SingleInstanceApplication()
            {
                base.IsSingleInstance = true;
            }
    
            public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
            {
                SingleInstanceApplication app = new SingleInstanceApplication();
                app.MainForm = f;
                app.StartupNextInstance += startupHandler;
                app.Run(Environment.GetCommandLineArgs());
            }
        }  
    }
    

    Thanks alot all, and especially the person who posted that link I mentioned above but I guess he deleted his answer?

    Regards, Kenny

提交回复
热议问题