0

I have been working on a project of making my own IP cam pop up.
I have this working but now want to start this from the command line but pass the specific camera ip in.
My programming skills are limited and am very new to C#
The code that takes the string is

this.textBox_IP.Text = "xxx.xxx.xxx.xxx"

I have been stuck on this for a few days now of how to pass the ip in, so at the cmdline

IpCam.exe xxx.xxx.xxx.xxx

Would anyone have a solution of how to go about this

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114

2 Answers2

8

The signature of the default startup method of a C# program is

static void Main(string[] args)

The command line arguments appear in args, so if you just pass one parameter

args[0]

would contain the IP address in your example.

It would be advisable to check

args.Length >= 1

to make sure that at least one parameter has been entered.

For more advanced advice on parsing the command line, see

Best way to parse command line arguments in C#?

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • @techno: Which part do you believe to be incorrect? It looks fine to me, and a comment like that with no detail is unhelpful. – Jon Skeet Jun 01 '12 at 08:20
  • 1
    @techno: That doesn't answer what you believe to be incorrect in *this* answer. Note that if you believe the first element of `args` to be the program name, you're *wrong*. It is when you fetch from `Environment.GetCommandLineArgs`, but not when you use the array passed into `Main`. – Jon Skeet Jun 01 '12 at 08:22
  • @JonSkeet I was wrong.Although since its a GUI app.Its better to do this from the Main Form Load Event by using string[] args = Environment.GetCommandLineArgs(); – techno Jun 01 '12 at 08:26
  • 1
    @techno: Why? Just because it's a GUI doesn't mean there isn't a Main method which can be used to get this information. By doing it there, you can isolate the difference between whether or not there's any initialization information to *just* the point of initialization of the program, rather than in the form itself. – Jon Skeet Jun 01 '12 at 08:28
  • This is working now, what i have now occurring is this. there is a connect button, on load of form this connect button thinks it has been clicked, thus trying to connect to the IP, however, the IP is not being passed in as an arg until after this routine has completed so it wont connect – user1430177 Jun 01 '12 at 10:12
  • I would suggest posting the full code in a separate question. – Eric J. Jun 01 '12 at 18:34
0

It is better to do this from the Main Form Load Event in GUI Apps Its simple just pass the ip as commandline argument eg:myprogram.exe yourip and you can get the ip from the arguments using

string[] args = Environment.GetCommandLineArgs();

But args[0] will not contain the ip,it will contain the your program name myprogram.exe To get the ip you will have to read the second argument

string myip=args[1];

Add this in Form Load

string myip=null;
string[] myargs = Environment.GetCommandLineArgs();
string myip=myargs[1];
textBox_IP.Text =myip;
techno
  • 6,100
  • 16
  • 86
  • 192
  • It's simpler to use the arguments passed into `Main` though, rather than retrieving them later... – Jon Skeet Jun 01 '12 at 08:20
  • Ok, when i put the above in i receive this error Error 1 A local variable named 'args' cannot be declared in this scope because it would give a different meaning to 'args', which is already used in a 'parent or current' scope to denote something else. As i say this is all new to me - again thanks for any help – user1430177 Jun 01 '12 at 08:32
  • @user1430177 change the name of 'args' to something else and try – techno Jun 01 '12 at 08:34
  • Hi again Techno, changing 'args' now builds but does not pass in the IP from the command line. do i need to add anything to the settings"command line arguments"? what i dont get is how those statements would populate this this.textBox_IP.Text = ""; as when leave the "" blank, the form does not have an IP even when passing in as an argument, obv. when i put am ip in the "" the form connects to my cam - thanks in advance – user1430177 Jun 01 '12 at 08:41
  • use this.textBox_IP.Text=myip – techno Jun 01 '12 at 08:45
  • You will have to put all these in the Main form load event – techno Jun 01 '12 at 08:50
  • hey Techno. error now received is Error 1 The name 'myip' does not exist in the current context. i have this.textBox_IP.Text = myip; declared in private void InitializeComponent() and string[] argu = Environment.GetCommandLineArgs(); string myip = argu[1]; declared in [STAThread] static void Main(string[] args) { - thanks again – user1430177 Jun 01 '12 at 09:04
  • oh man! Dont put anything in the InitializeComponent().Delete what you have added.Just double click the main form and add the following code.See the Edit – techno Jun 01 '12 at 09:07
  • Brilliant techno, that now passes my IP, however now there is a different problem, its passing my ip in to late, its trying to connect before the IP is passed using this private void button_connect_Click(object sender, System.EventArgs e){lblStatus.Text =""; if (m_deviceProxy == null) { try { – user1430177 Jun 01 '12 at 09:28
  • button_connect.Text = "Connecting"; button_connect.Enabled = false; m_deviceConnector.DefaultTimeout = 1000000; m_deviceConnector.ConnectAsync(this.textBox_IP.Text, ""); } catch (System.ArgumentException) { button_connect.Text = "Connect"; button_connect.Enabled = true; MessageBox.Show("Failed to connect! Please check URL.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } its trying to connect onload – user1430177 Jun 01 '12 at 09:28
  • is there any way of me putting the code down so u can ready it? – user1430177 Jun 01 '12 at 09:34