您可以通过以下两种方式之一来完成此操作。
第一种方法是使用string[] args
并将其传递给Main
给你的Form
,像这样:
// Program.cs
namespace MyNamespace
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm(args));
}
}
}
然后在MyForm.cs
请执行下列操作:
// MyForm.cs
namespace MyNamespace
{
public partial class MyForm : Form
{
string Title, Line1, Line2, Line3, Line4;
public MyForm(string[] args)
{
if (args.Length == 5)
{
Title = args[0];
Line1 = args[1];
Line2 = args[2];
Line3 = args[3];
Line4 = args[4];
}
}
}
}
另一种方法是使用Environment.GetCommandLineArgs()
,像这样:
// MyForm.cs
namespace MyNamespace
{
public partial class MyForm : Form
{
string Title, Line1, Line2, Line3, Line4;
public MyForm()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length == 6)
{
// note that args[0] is the path of the executable
Title = args[1];
Line1 = args[2];
Line2 = args[3];
Line3 = args[4];
Line4 = args[5];
}
}
}
}
你就会离开Program.cs
原来如此,没有string[] args
.