gpt4 book ai didi

c# - Windows 窗体应用程序命令行参数

转载 作者:太空狗 更新时间:2023-10-30 01:05:11 24 4
gpt4 key购买 nike

我通过 Google 进行了搜索,在这里找到了很多示例,但我似乎无法让我的 Windows 窗体应用程序运行并从命令行获取参数。我真的很想选择在没有控制台版本的情况下安排应用程序。但每次我设置 cmd 行参数时,它都会因 CLR20r3 错误而出错。

static void Main(string[] args)
{
if(args != null && args.Length > 0)
{
/*
* arg[1] = Backup Location *require
* arg[2] = Log File - Enable if present *optional
* arg[3] = Debug Messages - Enabled if present *optional
* arg[4] = Backup Type - Type to perform *required
*/

}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
Application.Run(new Form1());
}
}

每当我尝试传递一个 arg 时它都会出错 ex

myapp.exe "C:\Backup\" => CLR20r3

最佳答案

这是我在项目中使用的启动代码示例,该项目根据命令行参数作为表单应用程序或无表单应用程序运行。

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BuildFile
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
int ABCFile = -1;
string[] args = Environment.GetCommandLineArgs();
if ((args.Length > 1) && (args[1].StartsWith("/n")))
{
... unrelated details omiited
ABCFile = 1;
}
}

if (ABCFile > 0)
{
var me = new MainForm(); // instantiate the form
me.NoGui(ABCFile); // call the alternate entry point
Environment.Exit(0);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
}

请注意,这仅有效,因为我的替代入口点中没有任何内容依赖于运行时环境 Application.Run() 方法提供的事件等,其中包括处理 Windows 消息等。

关于c# - Windows 窗体应用程序命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19965311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com