silentthread2k
asked on
How can I read and pass a command line argument to a C# windows form?
I don't have the args variable that I normally do with a C# console window.....
private void Form1_Load(object sender, EventArgs e)
{
private void Form1_Load(object sender, EventArgs e)
{
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Whoops, forgot to include example code above:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (string args in Environment.GetCommandLine Args())
{
label1.Text = args;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (string args in Environment.GetCommandLine
{
label1.Text = args;
}
}
}
I believe carl_tawn solution is good one. recently I answered a similar question:
https://www.experts-exchange.com/questions/26827599/File-association-with-my-application.html
I just want to add to carl_tawn that you may need to pass arguments to a form, i.e. your form you start from Main may need to accept arguments string:
https://www.experts-exchange.com/questions/26827599/File-association-with-my-application.html
I just want to add to carl_tawn that you may need to pass arguments to a form, i.e. your form you start from Main may need to accept arguments string:
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args[0]));
}
To clarify: you need to pass args to a Form's constriuctor as above. Then you can do whatever you need.
Also, just in case - the Main method is located in the class Program.
Also, just in case - the Main method is located in the class Program.
Hi,
You can use Environment.CommandLine property where ever in the code to get the program name and any arguments specified on the command line when the current process was started.
Ex: instead of using string[] args alternatively u can use this property.
class Sample
{
public static void Main()
{
Console.WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
Console.WriteLine("Command Line: {0}", Environment.CommandLine);
}
}
You can use Environment.CommandLine property where ever in the code to get the program name and any arguments specified on the command line when the current process was started.
Ex: instead of using string[] args alternatively u can use this property.
class Sample
{
public static void Main()
{
Console.WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
Console.WriteLine("Command
}
}
Open in new window
You can then pass them to your form in its constructor.