简体   繁体   中英

How to create a command line to run c# exe file and give some parameters to a function in form1?

我有我想运行的 c# 程序,但是从 cmd 使用命令行所以这是我的问题我不知道如何创建命令,我不知道如何将命令中的参数发送到那里的某个函数.

You can use the Process class to execute files

        var fileName = "some.exe";
        var arguments = "";
        var info = new System.Diagnostics.ProcessStartInfo(fileName, arguments);
        info.UseShellExecute = false;
        info.CreateNoWindow = true;

        // if you want read output
        info.RedirectStandardOutput = true;
        var process = new System.Diagnostics.Process { StartInfo = info };

        process.Start();
        var output = process.StandardOutput.ReadToEnd();
        var error =  process.StandardError?.ReadToEnd();

You can create a console application and after generating (by building) .exe file, you can call with the arguments in command line.

Sample Example:

class Program
{
    static void Main(string[] args)
    {
        var a = Convert.ToInt32(args[0]);
        var b = Convert.ToInt32(args[1]);
        Console.WriteLine(a+b);
    }
}

OUTPUT

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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