Issue
I am trying to make an application. Can someone help me how to get command line parameters and put them into variables/strings. I need to do this on C#, and it must be 5 parameters.
The first parameter needs to be put into Title variable. The second parameter needs to be put into Line1 variable. The third parameter needs to be put into Line2 variable. The fourth parameter needs to be put into Line3 variable. And the fifth parameter needs to be put into Line4 variable.
Tank You For Helping!
Edit:
I need to add this into Windows Forms Application.
Solution
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
.
Answered By - Jashaszun Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.