如何将命令行参数传递给WinForms应用程序?
我有两个不同的WinForms应用程序,AppA & AppB。两者都运行.NET 2.0。如何将命令行参数传递给WinForms应用程序?
在AppA中,我想打开AppB,但我需要将命令行参数传递给它。我如何使用在命令行中传递的参数?
这是我目前在AppB中的主要方法,但我不认为你可以改变它?然后
static void main()
{
}
static void Main(string[] args)
{
// For the sake of this example, we're just printing the arguments to the console.
for (int i = 0; i < args.Length; i++) {
Console.WriteLine("args[{0}] == {1}", i, args[i]);
}
}
的参数将被存储在args
字符串数组中:
$ AppB.exe firstArg secondArg thirdArg
args[0] == firstArg
args[1] == secondArg
args[2] == thirdArg
可以通过访问Environment.CommandLine属性抓住任何.NET应用程序的命令行。它将命令行作为单个字符串,但解析出您正在查找的数据不应该非常困难。
拥有一个空的Main方法不会影响此属性或其他程序添加命令行参数的能力。
您可以使用这个签名:(在C#)静态无效的主要(字串[] args)
这篇文章可能有助于解释在编程的主要功能的作用,以及: http://en.wikipedia.org/wiki/Main_function_(programming)
这里对你是一个小例子:
class Program
{
static void Main(string[] args)
{
bool doSomething = false;
if (args.Length > 0 && args[0].Equals("doSomething"))
doSomething = true;
if (doSomething) Console.WriteLine("Commandline parameter called");
}
}
的最佳方式与args作为你的WinForms应用程序的工作是使用
string[] args = Environment.GetCommandLineArgs();
您可以将其与使用enum结合使用以巩固使用通过代码库的数组。
“你可以在你的应用程序中使用这个任何地方,你不只是 限制在一个控制台应用程序 在main()方法使用它像。”
在这里找到:HERE
这可能不是每个人的流行的解决方案,但我喜欢在Visual Basic应用程序框架,使用C#时也是如此。
增加提及Microsoft.VisualBasic
创建一个名为WindowsFormsApplication
public class WindowsFormsApplication : WindowsFormsApplicationBase
{
/// <summary>
/// Runs the specified mainForm in this application context.
/// </summary>
/// <param name="mainForm">Form that is run.</param>
public virtual void Run(Form mainForm)
{
// set up the main form.
this.MainForm = mainForm;
// Example code
((Form1)mainForm).FileName = this.CommandLineArgs[0];
// then, run the the main form.
this.Run(this.CommandLineArgs);
}
/// <summary>
/// Runs this.MainForm in this application context. Converts the command
/// line arguments correctly for the base this.Run method.
/// </summary>
/// <param name="commandLineArgs">Command line collection.</param>
private void Run(ReadOnlyCollection<string> commandLineArgs)
{
// convert the Collection<string> to string[], so that it can be used
// in the Run method.
ArrayList list = new ArrayList(commandLineArgs);
string[] commandLine = (string[])list.ToArray(typeof(string));
this.Run(commandLine);
}
}
类修改您的main()例程,看起来像这样
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var application = new WindowsFormsApplication();
application.Run(new Form1());
}
}
这种方法提供了一些额外的有用的功能(像SplashScreen支持和一些有用的事件)
public event NetworkAvailableEventHandler NetworkAvailabilityChanged;d.
public event ShutdownEventHandler Shutdown;
public event StartupEventHandler Startup;
public event StartupNextInstanceEventHandler StartupNextInstance;
public event UnhandledExceptionEventHandler UnhandledException;
考虑你需要开发一个程序,通过它你需要传递两个参数。首先,您需要打开Program.cs类,并在Main方法中添加参数,如下所示,并将这些参数传递给Windows窗体的构造函数。
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args[0], Convert.ToInt32(args[1])));
}
}
在窗口形式类中,添加从方案类接受的输入值作为像下方的参数的构造函数。
public Form1(string s, int i)
{
if (s != null && i > 0)
MessageBox.Show(s + " " + i);
}
要测试此操作,可以打开命令提示符并转到放置此exe文件的位置。给出文件名,然后给出parmeter1 parameter2。例如,见下面
C:\MyApplication>Yourexename p10 5
从上面的C#代码,它会提示一个MessageBox与值p10 5
。
输入:“whatever.exe -v foo/lol nisp”。输出:args [0] =“-v”; args [1] =“foo”; args [2] =“/ lol”; args [3] =“nisp”;有什么更容易? – 2009-07-24 19:22:45
@AlexeiLevenkov:非常感谢!几个月前有人编辑过,而我没有意识到这是错误的。使用上面的示例代码进行验证,实际上,第一个数组条目是第一个参数,并且可执行文件的名称不会输入图片。 – Thomas 2016-05-23 08:45:55
不能相信我在一整年后看到'string [] args'这么多次,它从来没有发生过我现在一直到现在! haha – Niklas 2016-06-28 18:31:35