0

I need to call a program from cmd using an array of numbers(mandatory) and an int time(optional). I have never done this so i'm a bit shaky on the details.

The path is D:\Robert\FactorialConsoleApplication\FactorialConsoleApplication\bin\Debug\FactorialConsoleApplication.exe

As you can tell, the program calculates the factorial of the numbers in the array. The int time is used as a delay in order to display the progress of the stack.

How do I call the program with parameters? Thanks in advance!

P.S. Here is some of the code

class Program
{
    public static void Progress(ProgressEventArgs e)
    {
        int result = e.getPartialResult;
        int stack_value = e.getValue ;
        double max = System.Convert.ToDouble(numbers[j]);
        System.Convert.ToDouble(stack_value);
        double percent = (stack_value / max) * 100;

        Console.CursorLeft = 18;
        Console.Write(result + " ");
        Console.CursorLeft = 46;
        Console.Write(System.Convert.ToInt32(percent) + "%      ");

    }
    public static void Calculate(int number, int time=0)
    {

        Factorial Fact = new Factorial();
        Fact.Progression += new Factorial.ProgressEventHandler(Progress);
        Console.Write("\n" + "Partial results : ");
        Console.CursorLeft = 35;
        Console.Write("Progress : ");         
        int Result = Fact.CalculateFactorial(number, time);
        Console.WriteLine(" ");
        Console.WriteLine("The factorial of " + number + " is : " + Result);


        Console.ReadLine();
    }

    static int j;
    static int[] numbers;


    public static void Main(string[] args)
    {
        int i=0;
        bool ok = false;
        string line = string.Empty;
        numbers = new int[10];
        Console.Write("Please insert wait time (0,1 or 2) : ");
        int time = int.Parse(Console.ReadLine()) * 1000;
        Console.Write("Please insert a number : ");
        do
        {
            line = Console.ReadLine();
            if (line != "")
            {
                i++;
                numbers[i] = int.Parse(line);


            }
            else
            {
                ok = true;
            }
        }
        while (ok == false);
        for (j = 1; j <= i; j++)
        {

            Calculate(numbers[j],time);
        }

    }
}
robertpas
  • 643
  • 5
  • 12
  • 25
  • 1
    See this: http://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c – Indy9000 Aug 01 '12 at 12:41

2 Answers2

0

In .net you can use Process.Start from System.Diagnostics to launch an application, you can pass parameters too

For example Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); will open Internet Explorer and pass the value "C:\\myPath\\myFile.htm" as parameter to it.For more examplles check the MSDN article on Process.Start method

Update If in case you are looking to take parameters to your application, when launching itself you don't have to do anything, you are already doing that, the parameter args in your Main method will hold the arguments passed to your application, just try and parse those values in the args array to int array and you are good to go.

Vamsi
  • 4,237
  • 7
  • 49
  • 74
0

Ok, so here is the solution.

I used an args parser like this :

static int extra;
    public static void Main(string[] args)
    {

        foreach (string s in args)
        {
            extra = int.Parse(s);


            Calculate(extra);
        }
     }

And I changed :double max = System.Convert.ToDouble(numbers[j]); to :double max = System.Convert.ToDouble(extra);

To call it I open cmd in the directory where the exe is and I type :

Program.exe 3 4 5

It will calculate the factorials of 3, 4 and 5 respectively

robertpas
  • 643
  • 5
  • 12
  • 25