Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行用C制作的EXE文件格式CMD#_C#_Cmd_Exe_Argument Passing - Fatal编程技术网

C# 运行用C制作的EXE文件格式CMD#

C# 运行用C制作的EXE文件格式CMD#,c#,cmd,exe,argument-passing,C#,Cmd,Exe,Argument Passing,我是c#的新手,我被这个难题困住了 我最近用c#编写了一个Gui程序,其中包括几个选项卡和一些其他东西 现在我想将其中一个选项卡制作成一个exe文件,我可以通过cmd运行它。 我想放在文件中的整个代码由一个类组成 诸如此类 class E2p { main program( take 2 arg ) {some Code make a CSV file in appDirectory } 我想把它转换成EXE文件,这样我就可以像那样从CMD运行它了 E2pChck.exe -i 10.0.

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

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

我该怎么做

我不是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"

我希望这对您有所帮助。

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

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

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

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

希望有帮助

为什么不创建一个新的项目,一个Windows控制台应用程序,这样做呢?还是我误解了你的意图?你需要截取其他程序的输出吗?。如果是这样:@JimMischel你是说詹姆斯回答了什么?如果不是,你能举个简单的例子吗?@thepirat000不,我是说James回答的:)你甚至可以让GUI程序用System.Diagnostics.Process启动新的exe(它可以添加命令行参数)。@LordTakkera我不知道你的意思是什么,您能给出一个简单的例子吗?MSDN页面底部有几个System.Diagnostics.Process示例基本上您可以从程序内部启动另一个程序(程序)。因此,如果您的Windows窗体程序上有一个按钮,当您按下该按钮时,它可以启动另一个命令行程序(如果您需要,也可以启动Winforms程序)。看看这些例子。@user3134181我在下面的答案中贴了一个简单的例子,希望你能发现它有用。关于这一点,其他地方也有很多很好的问题/答案。@LordTakkera谢谢你的帮助:)我想你的意思是你不会把它放在你的按钮处理程序中。好吧,你可以在按钮处理程序中启动线程,但是的,你不想在等待进程完成时锁定UI。谢谢你的澄清。
System.Diagnostics.Process.Start("E2pChck.exe", "-i 10.0.0.127 -r RandomWord");