Labels

Friday, August 19, 2016

How to pass parameter to an Exe

How do pass parameter to EXE


We can use below mention method pass parameter to an Exe from different application. WPF application  or Windows form application , we can use common way to do it.


1. Add reference System.Diagnostics your class.                                                                

2. Then need start process.                                                                                    
                                                                                          ProcessStartInfo processStrInfo = new ProcessStartInfo();
              processStrInfo.FileName =   /// Exe path;
              processStrInfo.Arguments =  /// Parameter to pass as string;
              processStrInfo.UseShellExecute = false;
              Process.Start(processStrInfo);                                                                                                              
3. parameter can send as string.

           string tempCommandArgs ="TestPara";                 


             ProcessStartInfo processStrInfo = new ProcessStartInfo();
             processStrInfo.FileName = "test.exe";  /// Exe path;
             processStrInfo.Arguments = tempCommandArgs  /// Parameter to pass as string;
             processStrInfo.UseShellExecute = false;
             Process.Start(processStrInfo);   

4 .When we passing multiple parameter, we can use space between parameters .

          tempCommandArgs  =COMPANYNAME +" "+USERID

5. If parameter value have spaces.
    ex - COMPANYNAME  can be "Test Company"
   
    In this time we can use below method (add stroke to start of parameter and end of parameter)

    tempCommandArgs  ="\"" + COMPANYNAME + "\"" + " "
                                                  +"\"" + USERlOGINNAME. + "\"";    

Note:-
This is way to pass parameter to Exe as argument , But running exe from another running application (exe) is not good practice. It will consume some more memory

Now take a look at how to consume passed argument form exe.

Note:-
This example I have done with WPF application

1. We can override OnStartup in App.xaml.cs

 protected override void OnStartup(StartupEventArgs e)
  {          
            string[] args = Environment.GetCommandLineArgs();
            args = e.Args;
         
            if (args.Count() > 0)
            {            
                COMPANYNAME = args[0];        
                USERNAME = args[1];
            }

 }










No comments:

Post a Comment