我有两个不同的WinForms应用程序,AppA和AppB。两者都在运行.NET 2.0。
在AppA中,我想要打开AppB,但我需要传递命令行参数给它。如何使用传入的命令行参数?
这是我目前在AppB中的主方法,但我认为你不能更改它?
static void main()
{
}
我有两个不同的WinForms应用程序,AppA和AppB。两者都在运行.NET 2.0。
在AppA中,我想要打开AppB,但我需要传递命令行参数给它。如何使用传入的命令行参数?
这是我目前在AppB中的主方法,但我认为你不能更改它?
static void main()
{
}
与您的WinForms应用程序一起使用参数的最佳方法是使用
string[] args = Environment.GetCommandLineArgs();
你可能可以将这个方法与使用枚举相结合,以巩固在整个代码库中对数组的使用。
“而且你可以在应用程序的任何位置使用它,不仅仅像在控制台应用程序的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
Console
,用户功能需要从主窗体运行。 - ivan_pozdeevConsole
只是代码中的示例,如代码中所注释的那样。 - Thomas假设您需要开发一个程序,需要传递两个参数。首先,您需要打开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])));
}
}
在Windows Form类中,添加一个带参数的构造函数,该构造函数可以从 Program 类接受输入值,如下所示。public Form1(string s, int i)
{
if (s != null && i > 0)
MessageBox.Show(s + " " + i);
}
为了测试这个,您可以打开命令提示符并转到放置该exe文件的位置。给出文件名,然后是参数1和参数2。例如,参见下面:
C:\MyApplication>Yourexename p10 5
根据上面的C#代码,它会弹出一个带有值p10 5
的Messagebox。
通过访问Environment.CommandLine属性,您可以获取任何.Net应用程序的命令行。它将作为一个单一字符串呈现,但解析出所需数据不应该太困难。
拥有空的Main方法不会影响此属性或其他程序添加命令行参数的能力。
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");
}
}
这可能不是每个人都喜欢的解决方案,但我喜欢在使用C#时使用Visual Basic中的应用程序框架。
添加对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());
}
}
public event NetworkAvailableEventHandler NetworkAvailabilityChanged;d.
public event ShutdownEventHandler Shutdown;
public event StartupEventHandler Startup;
public event StartupNextInstanceEventHandler StartupNextInstance;
public event UnhandledExceptionEventHandler UnhandledException;
main(string[] args)
相比,是否有任何好处? - Adjit