我希望能够通过命令行对我的Windows窗体应用程序进行编程运行。为此,我已经将逻辑从窗体中分离出来,并放在了一个独立的类中。现在,我卡在了如何根据命令行参数的存在与否来切换应用程序上。
这是主类的代码:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1) // gets passed its path, by default
{
CommandLineWork(args);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static void CommandLineWork(string[] args)
{
Console.WriteLine("It works!");
Console.ReadLine();
}
这里的 Form1
是我的窗体,It works!
只是实际逻辑的占位符。
现在,在 Visual Studio 中运行此代码(带有命令行参数)时,将向输出打印出 It works!
。但是,当从 /bin/Debug/Program.exe 文件(或 /Release 文件)中运行时,应用程序会崩溃。
我这样做正确吗?是否更明智(即需要更少的开发者时间)让我的逻辑类成为一个 DLL,由两个独立的应用程序加载?还是有其他我不知道的完全不同的方法?
提前感谢!