简体   繁体   English

C#使用cmd启动带有强制参数和可选参数的程序

[英]C# launch a program with mandatory parameters and optional parameters from cmd

I need to call a program from cmd using an array of numbers(mandatory) and an int time(optional). 我需要使用数字数组(强制)和整数时间(可选)从cmd调用程序。 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 路径为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. int时间用作延迟,以显示堆栈的进度。

How do I call the program with parameters? 如何使用参数调用程序? Thanks in advance! 提前致谢!

PS Here is some of the code PS这是一些代码

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);
        }

    }
}

In .net you can use Process.Start from System.Diagnostics to launch an application, you can pass parameters too 在.net中,您可以使用System.Diagnostics中的Process.Start启动应用程序,也可以传递参数

For example Process.Start("IExplore.exe", "C:\\\\myPath\\\\myFile.htm"); 例如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 将打开Internet Explorer并将值"C:\\\\myPath\\\\myFile.htm"作为参数传递给它。有关更多示例,请参阅Process.Start方法上的MSDN文章。

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. 更新如果万一您想为应用程序添加参数,则在启动自身时无需执行任何操作,您已经在执行此操作,Main方法中的参数args将保存传递给应用程序的参数,只需尝试并将args数组中的这些值解析为int数组,您就可以开始了。

Ok, so here is the solution. 好的,这就是解决方案。

I used an args parser like this : 我使用了像这样的args解析器:

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]); 我改变了: double max = System.Convert.ToDouble(numbers[j]); to : double max = System.Convert.ToDouble(extra); 到: double max = System.Convert.ToDouble(extra);

To call it I open cmd in the directory where the exe is and I type : 要调用它,我在exe所在的目录中打开cmd,然后键入:

Program.exe 3 4 5 程序3 4 5

It will calculate the factorials of 3, 4 and 5 respectively 它将分别计算3、4和5的阶乘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM