【问题标题】:Running EXE file form CMD made with C#使用 C# 从 CMD 运行 EXE 文件
【发布时间】:2014-03-26 15:26:54
【问题描述】:

我是 c# 的新手,我被这个难题困住了
我最近用 c# 制作了一个 Gui 程序,其中包括几个选项卡和其他一些东西 现在我想将其中一个选项卡制作为我可以通过 cmd 运行的 exe 文件。 我要放入文件的整个代码由一个类组成 类似的东西

class E2p
{
main program( take 2 arg )
{some Code


make a CSV file in appDirectory
}

我想把它转成 EXE 文件,这样我就可以像这样从 CMD 运行它了

E2pChck.exe -i 10.0.0.127 -r RandomWord

我该怎么做??

【问题讨论】:

  • 为什么不创建一个新项目,一个 Windows 控制台应用程序,这样做呢?还是我误解了您要做什么?
  • 你需要拦截其他程序的输出吗?如果是这样:stackoverflow.com/questions/415620/…

标签: c# cmd exe argument-passing


【解决方案1】:

我不是 100% 确定你在追求什么,但我认为你的意思是你希望能够通过几个参数从命令行运行你的 exe。

这些参数通过Main 方法传递到您的应用程序中,您可以在 Program.cs 中找到该方法。在命令行应用程序中为您提供了 arguments 参数,但您可以将其添加到 Windows 窗体应用程序中。

class Program
{
    static void Main(string[] args)
    {
        string firstArgument;
        string secondArgument;
        const int NumberOfArgumentsRequired = 2;

        // you can access the arguments using the args array, 
        // but we need to make sure we have enough arguments, 
        // otherwise we'll get an index out of range exception 
        // (as we're trying to access items in an array that aren't there)
        if (args.Length >= NumberOfArgumentsRequired)
        {
            firstArgument = args[0];
            secondArgument = args[1];
        }
        else
        {
            // this block will be called if there weren't enough arguments
            // it's useful for setting defaults, although that could also be done
            // at the point where the strings were declared
            firstArgument = "This value was set because there weren't enough arguments.";
            secondArgument = "So was this one. You could do this at the point of declaration instead, if you wish.";
        }

        string outputString = string.Format("This is the first: {0}\r\nAnd this is the second: {1}", firstArgument, secondArgument);
        Console.WriteLine(outputString);
        Console.ReadKey();
    }
}

如果您在命令行中输入E2pChck.exe -i 10.0.0.127 -r RandomWord,则:

args[0] would be "-i"
args[1] would be "10.0.0.127"
args[2] would be "-r"
args[3] would be "RandomWord"

【讨论】:

    【解决方案2】:

    我知道这在技术上并不能回答问题,但 OP 要求提供启动流程的示例。

    您可以将此代码放在您的按钮处理程序中(可能放在单独的线程上,这样您的 UI 就不会挂起)

        System.Diagnostics.ProcessStartInfo csvGenerationProcInfo = new System.Diagnostics.ProcessStartInfo();
        csvGenerationProcInfo.Arguments = "-i 10.0.0.127 -r RandomWord";
        csvGenerationProcInfo.FileName = "E2pChck.exe";
    
        System.Diagnostics.Process csvGenerationProc = System.Diagnostics.Process.Start(csvGenerationProcInfo);
        csvGenerationProc.WaitForExit();
    

    或者,如果您不需要 ProcessStartInfo 的所有功能,您可以使用:

    System.Diagnostics.Process.Start("E2pChck.exe", "-i 10.0.0.127 -r RandomWord");
    

    希望有帮助!

    【讨论】:

    • 我认为你的意思是你不会把它放在你的按钮处理程序中。
    • 好吧,您可以在按钮处理程序中启动线程,但是是的,您不希望在等待进程完成时锁定 UI。谢谢你的澄清。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2019-12-25
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多