You can do it in one of two ways.
The first way is to use string[] args
and pass that from Main
to your Form
, like so:
// 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));
}
}
}
And then in MyForm.cs
do the following:
// 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];
}
}
}
}
The other way is to use Environment.GetCommandLineArgs()
, like so:
// 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];
}
}
}
}
and you would just leave Program.cs
how it was originally, without the string[] args
.