0

Possible Duplicate:
How to get command line from a ClickOnce application?

I was working on a console application and manually added the string[] args inside of Main() after I had already done a bunch of other work. Is that all I have to do to accept command line arguments? Or do I need to configure something elsewhere also? I keep doing Console.WriteLine("{0}",args.Length) and get zero no matter what I send after the exe..

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", args.Length);
    }
}

then I run ...\setup.exe yes no maybe and get 0 for length. What more do I need to do?

MORE INFO:

I tried to break after setting command line arguments in the properties page and I get the following error.:

enter image description here

I am thinking that someone's comment about ClickOnce deployment is my problem. How can I deploy in VS2010 to allow this?

MORE INFO:

I disabled "ClickOnce security settings" under Properties -> Security and was able to debug successfully, but when I click on Publish, it automatically turns this setting back on.. How do I prevent that?

Community
  • 1
  • 1
Andy Raddatz
  • 2,792
  • 1
  • 27
  • 29
  • 1
    @DJKRAZE: Not necessary. You can use any format of arguments (dashes, slashes, name it) and it will work anyway. – abatishchev Sep 19 '12 at 19:34
  • @Andy: Does `Console.WriteLine(args.Length)` not work too? – abatishchev Sep 19 '12 at 19:34
  • 3
    You're doing the right thing. Break with a debugger and check out the value of args (don't forget to set the command line arguments from the project properties page) – zmbq Sep 19 '12 at 19:36
  • 2
    by the way, if you have a clickonce deployment, you can't access the command line in that way – user287107 Sep 19 '12 at 19:36
  • 3
    Just to be sure, this Main() is from setup.exe, right? – H H Sep 19 '12 at 19:46
  • @HenkHolterman Yes, setup.exe is the output from my Publish in VS2010, and Main is inside `class Program {}`.. I'll update my question to include that. – Andy Raddatz Sep 19 '12 at 19:51
  • Have you tried using [`Environment.GetCommandLineArgs()`](http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx)? – Uwe Keim Sep 19 '12 at 19:55
  • It is about ClickOnce. See linked duplicate. – H H Sep 19 '12 at 20:10
  • Probably you need to supply in a URL. Check this http://stackoverflow.com/questions/429351/how-does-one-pass-command-line-argument-to-a-clickonce-application – prashanth Sep 19 '12 at 20:16
  • About how to disable ClickOnce, I suggest you create a new question for that. – Fernando Correia Sep 19 '12 at 20:42
  • @HenkHolterman you are correct, it is ClickOnce, but I've read through the duplicate and the help links and can't figure out how to use `ActivationArguments.ActivationData` to get the args.. Are the args passed in to setup.exe available somehow through this class? – Andy Raddatz Sep 19 '12 at 20:42
  • _When_ do you want the arguments? During deploy or later when the app starts locally? @prashanth has another link. – H H Sep 19 '12 at 20:51
  • Later, when it is run. I am actually deploying to a shared directory and running via command-line from another machine (using the "Online only" publish option in VS). Everything was great until I wanted to add the command line args.. – Andy Raddatz Sep 19 '12 at 21:14

2 Answers2

3

This example:

using System;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Number of command line parameters = {0}", args.Length);
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
            }
        }
    }
}

When executed as ConsoleApplication1.exe a b c will output:

Number of command line parameters = 3
Arg[0] = [a]
Arg[1] = [b]
Arg[2] = [c]

See Command Line Parameters Tutorial

Update: this code

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("{0}", args.Length);
    }
}

When executed as ConsoleApplication1.exe a b c outputs

3

Above all, make sure you're executing the correct .exe.

Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
  • the only difference to what I have is you use `internal` for `class Program` and `private` for `static void Main(string[] args)`. Would those affect my situation at all? – Andy Raddatz Sep 19 '12 at 20:02
  • No, those modifiers won't affect the result; see updated example in my answer. I suggest you start a new console project and paste the above code. Also, make sure you're actually passing the parameters. I used cmd.exe and ran the generated .exe file in the command prompt. – Fernando Correia Sep 19 '12 at 20:36
  • 1
    I've found it's happening because of ClickOnce deployment. I'm passing params to the setup.exe, not my compiled .exe... – Andy Raddatz Sep 19 '12 at 20:38
  • Right, I added that hint to my answer as well. – Fernando Correia Sep 19 '12 at 20:41
  • I think the answer explains what he has to do to accept command line arguments. About how to pass them using ClickOnce, [this answer](http://stackoverflow.com/a/430536/376366) might help. – Fernando Correia Sep 19 '12 at 20:57
  • 1
    It's correct in that I was targeting the wrong .exe. I didn't know enough about ClickOnce at all so my question has kind of spiderwebbed into a ClickOnce security/.exe targeting question. I'm trying to research ClickOnce right now and come up with a better question, if necessary. I'm a little new to SOF question-asking, so what should I do when my question completely changes, delete the original question? Sorry, trying to get work done and follow SOF protocol at the same time, haha – Andy Raddatz Sep 19 '12 at 21:09
0

Perhaps you can do something like this in a for loop to check what the values if any of the command arguments are

public class CountCommandLineArgs
{
   public static void Main(string[] args)
   {
       Console.WriteLine("Number of command line parameters = {0}",
          args.Length);
       foreach(string s in args)
       {
          Console.WriteLine(s);
       }
   }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • When `args.Lenght == 0` then this won't Write much. – H H Sep 19 '12 at 19:45
  • assuming he doesn't pass command line params you're correct but I am going under the assumption that he is passing params according to MSDN this is one approach http://msdn.microsoft.com/en-us/library/aa288457%28v=vs.71%29.aspx – MethodMan Sep 19 '12 at 20:07