Newbie in C# Using Agruments
Hi I need to call my Program like this:
C:\program.exe name
And I want to show the Agrument name in a label.
I tryed to do this like this:
Code:
namespace HelloWorld
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
lblMsg.Text = "Hello" + e + "to this world!";
}
}
}
I figured out that this was wrong..
I didn't find anything on google.
And I asked my C# Teacher.. And she had NO CLUE were I was talking about..
(Ok, she is really a noob, she only knows or textbook inside - out but ok..)
Is here ayone how can help me out ^^?
Re: Newbie in C# Using Agruments
The command-line arguments are not sent to any events, they are sent to the static Main() procedure of your startup class, if you define one that has a string[] argument.
Code:
class MyProgram
{
[STAThread] static void Main(string[] args)
{
// ...
}
}
You can also access them using the static Environment.GetCommandLineArgs() method.
Code:
lblMsg.Text = "Hello" + string.Join(", ", Environment.GetCommandLineArgs()) + "to this world!";
Re: Newbie in C# Using Agruments
So in Program.cs I need to change:
Code:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
into
Code:
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
this
And then I need to send the var args to my Form1.cs?
Thanks ^^
Re: Newbie in C# Using Agruments
Yeah you can do that by passing them to the frmMain constructor or you can use Environment.CommandLine like I showed in my second example. Whichever way you choose is up to you.