Erroldactyl
Erroldactyl

Reputation: 383

C# Console Application Does NOT read from the command line, as it should

I have spent a good while trying to get this simple code to read from the command line:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length > 0)
            {
                for (int i = 0; i < args.Length; ++i)
                    System.Console.WriteLine(args[i]);
            }
            else System.Console.WriteLine("NO COMMAND INPUT DETECTED");
            System.Console.ReadLine();
        }
    }
}

When typing the command:

ConsoleApplication3.application "pleasework"

I get the following message in the command line:

NO COMMAND INPUT DETECTED

indicating that the command line is not working properly. Any thoughts? I am very bad with Visual Studio (this is 2012) so I imagine there is some special property I need to change or something ridiculous.

Thanks!

Upvotes: 0

Views: 2607

Answers (2)

ClosedEyesSeeing
ClosedEyesSeeing

Reputation: 335

Using this:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(args.Length);

            Console.Read();
        }
    }
}

I was able to go to my command prompt and run:

C:\ProjectPath\ConsoleApplication1\bin\debug\ConsoleApplivation1.exe "Test" "Test2"

With a result of:

2

Edit: It looks like you are running a ClickOnce Console Application. This complicates what you want to do, but it isn't impossible. Here are several resources that discuss this particular issue:

Processing Command Line Arguments in an Offline ClickOnce Application

Harvesting ClickOnce Command Line Arguments

How to pass arguments to an offline ClickOnce application

Upvotes: 1

Yair Nevet
Yair Nevet

Reputation: 13003

Your code seems to be okay. You're probably not passing the argument properly.

Take the following steps:

Right-Click on your Project => Properties => Debug => insert "Pleasework" into the command line arguments => save and debug your code step-by-step.

enter image description here

And you'll see:

enter image description here

Upvotes: 1

Related Questions