您可以通过以下两种方式之一进行操作。
第一种方法是使用 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。