Starting EXE with args

NachitoMax 411 Reputation points
2022-04-15T23:08:07.21+00:00

Hi

I need to start an EXE with args that will run some validation as it starts. I sort of understand the Process.Start(args) but not sure if its this that i need.

I need to to know 2 things

  1. how to call the exe correctly with the args
  2. where in the exe do i put the code to receive the args

The validation is basically do not run if you dont receive the correct args as it needs to be called by a 3rd party application in order to work.

Thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2022-04-16T20:16:59.157+00:00

    See Process.Start Method. Part of one of the examples is:

    Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");  
    

    In that, C:\\myPath\\myFile.htm is the arguments.

    See Main() and command-line arguments. Try out that sample program. In Visual Studio you can specify some arguments to use for testing purposes by going to the project's properties then go to Debug and in Start options is Command line arguments. So you can put some sample arguments there and see what they look like in execution. Then you can modify that program to do whatever processing you need and when it works you have sample code ready for use.

    0 comments No comments

  2. Bruce (SqlWork.com) 56,686 Reputation points
    2022-04-17T22:07:26.517+00:00

    more detail:

    typical spawn:

    Process.Start("myapp.exe", "-l foo1.txt foo2.txt");  
    

    .net 6 my app

    var log = false;  
    var fileNames = new List<string>();  
      
    foreach (var arg in args)  
    {  
        if (arg == "-l")   
            log = true;  
        else  
            fileNames.Add(arg);  
    }  
      
    Console.WriteLine($@"log={log} fileNames={string.Join(",",fileNames)}");  
    

    note: like unix utilities arg[0] is not the executable name

    0 comments No comments