locked
Running an C# console app from command line and passing args... RRS feed

  • Question

  • Hi,


      I have an console app (which is build and has an exe) which i need to execute through command line and pass some args..

    this is few lines of my code..


     class Program
        {
            static void Main(string[] args)
            {
                string[] _questionCodes = { "1033" };//GetDistinctQuestionCodes();
    
                foreach (string _questionCode in _questionCodes)
                {
                    string[] _conceptCodes = GetDistinctConceptCodes(_questionCode);                
    
                    foreach (string _conceptCode in _conceptCodes)
                    {
                        string _highestLevelMap = GetHighestMapLevelQuestion(_questionCode);
                        // 1 -- concept 2-- Department 3--SubDepartment--4--Class
                       
                        switch (_highestLevelMap)
                        {
                            case "1":
                                ProcessConcept(_questionCode, _conceptCode, _highestLevelMap);
                                break;
                            case "2":
                                ProcessDepartmentFromDecider(_conceptCode, _questionCode);
                                break;
                            case "3":
                                ProcessSubDepartmentFromDecider(_conceptCode, _questionCode);
                                break;
                            case "4":
                                ProcessClass(_questionCode, _conceptCode, _highestLevelMap);
                                break;
                            default:
                                Console.Write("Invalid Highest Level Map for Question Code {0}", _questionCode);
                                break;
    
                        }
                    }
    
                }
            }

    i need to pass the question codes from command line as argument.Right now i have commented the
    GetDistinctQuestionCodes() method and passed the hardcoded value which i need to do through commandline with out commenting the method.


    Wednesday, March 28, 2012 4:11 PM

Answers

  • You can give command line arguments in 2 ways.

    1. If you want to give command line arguments in Visual Studio then open project properties and debug tab you will find 'Command Line Arguments' textBox where you have to give your arguments.

    2. You can also give arguments on command prompt. Open command prompt and change directory to the directory where your exe is present. then on command prompt type your exe name followed by arguments seperated by space


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by RAGS1109 Wednesday, March 28, 2012 6:20 PM
    Wednesday, March 28, 2012 5:37 PM

All replies

  • If you just want to read the command line args passed into you program, just use the variable 'args' that's passed into the main method.  It's that simple.

    _questionCodes = args;

    • Proposed as answer by Sumit Vijay Monday, February 8, 2016 2:14 PM
    Wednesday, March 28, 2012 4:14 PM
  • how do i pass the arguments in command line...
    • Edited by RAGS1109 Wednesday, March 28, 2012 5:00 PM
    Wednesday, March 28, 2012 4:20 PM
  • You can give command line arguments in 2 ways.

    1. If you want to give command line arguments in Visual Studio then open project properties and debug tab you will find 'Command Line Arguments' textBox where you have to give your arguments.

    2. You can also give arguments on command prompt. Open command prompt and change directory to the directory where your exe is present. then on command prompt type your exe name followed by arguments seperated by space


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by RAGS1109 Wednesday, March 28, 2012 6:20 PM
    Wednesday, March 28, 2012 5:37 PM
  • I'd like to add the fact that none of the files that get published will run directly with command line arguments.  (My application has Online only as a property for publishing.)  The actual application .exe file has to be copied from Projects\Application\Application\bin\Debug to the production directory in order to accept command line arguments.  For this reason, I am doing a build, but not a publish when I make changes.
    Tuesday, March 31, 2015 1:59 PM
  • I have been able to do what you described successfully.  I had to include one of the dll's in order to make it work.  I have another project that is requiring more dll's to be added manually, and it is getting messy.  Your advice in this matter would be welcome.

    Wednesday, January 24, 2018 5:33 PM