C# pass the paramters to runnning process.

LWY 1 Reputation point
2022-08-25T02:31:21.973+00:00

Prevent the same process from running.
And when the process is running,
I want to pass parameters to the running process.

But I don't know how.

   static void Main(string[] args)  
    {  
        Process[] processes = null;  
        string strCurrentProcess = Process.GetCurrentProcess().ProcessName.ToUpper();  
        processes = Process.GetProcessesByName(strCurrentProcess);  

        if (processes.Length > 1)  
        {  
            if (args.Length > 1)  
            {  
                //Pss Paramater to running process  
                return;  
            }  
            else  
            {  
                return;  
            }  
        }  
        else  
        {  
            if (args.Length > 1)  
            {  
                Application.Run(new FrmMain(args));  
            }  
            else  
            {  
                Application.Run(new FrmMain());  
            }  
        }  
    }
Developer technologies Windows Forms
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-08-25T07:08:04.383+00:00

    @LWY , Welcome to Microsoft Q&A, We could use command line to pass the parameters to running process.

    First, I make the following code to check if the parameters' count is more than 1.

    Code example:

     static void Main(string[] args)  
            {  
                Application.EnableVisualStyles();  
                Application.SetCompatibleTextRenderingDefault(false);  
                if (args.Length> 1)  
                {  
    
                    Application.Run(new Form1());  
                }  
                else  
                {  
                    Application.Run(new Form2());  
                }  
            }  
    

    Second, Please try the following command to pass the parameters to winform project:

    CD E:\WindowsFormsApp1\bin\Debug (Your project path)  
      
    E:\WindowsFormsApp1\bin\Debug>WindowsFormsApp1.exe test1 test2  
      
    E:\WindowsFormsApp1\bin\Debug>WindowsFormsApp1.exe test1  
    

    Result1:

    234786-image.png

    Result2:

    234803-image.png

    Hope my answer could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Sheng Jiang 蒋晟 206 Reputation points
    2022-08-25T23:42:45.467+00:00

    You cannot prevent the same application from running. What you can do is instead of creating a new form in your new process, look for an existing form and if found, send the command line argument to the existing form before exiting.

    Your existing form then need to process that notification (e.g. open the file passed in the arguments and restored from minimized if necessary).

    There is an implementation in WindowsFormsApplicationBase (requires referencing Microsoft.VisualBasic.Forms.dll). For an example check https://www.hanselman.com/blog/the-weekly-source-code-31-single-instance-winforms-and-microsoftvisualbasicdll. If you are interested in writing your own, you can use a mutex and a named pipe (https://www.autoitconsulting.com/site/development/single-instance-winform-app-csharp-mutex-named-pipes/).

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.