Console program with multiple parameters - c#

I am currently writing a console program using .NET Core for Windows and want to execute certain functions with parameters, for example program.exe /a /b. Unfortunately this does not work as I thought and therefore only the first specified parameter is executed, but not several in a row.
I have already tried the following and have tried to change it minimally several times:
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please give me one or more parameters to continue.");
return;
}
var command = args[0];
switch (command)
{
case "/?":
Console.WriteLine("Some help text ..");
break;
case "/a":
Console.WriteLine("a");
break;
case "/b":
Console.WriteLine("b");
break;
}
}
Any idea(s) for me?

Related

How do I stop my C# console program from stopping when executing "console.readline();" twice

i'm really new to C# and i've been working on this really simple command line style program (that has custom commands and such). Now the commands work great but every time I allow the user to go back to enter another command or just anything it closes the program when I press enter. But only the second time I execute a command. I think this has something to do with console.WriteLine();
Here's my code (I've searched everywhere on how to fix this and nothing that i've found has worked)
using System;
namespace ConsoleProgram
{
class Program
{
private static string userEnteredCommand;
static void Main(string[] args)
{
Console.Title = "IAO Systems Service Console";
onCommandLineStart();
void onCommandLineStart()
{
Console.WriteLine("Copyright (C) 2018 IAO Corporation");
Console.WriteLine("IAO Systems Service Console (type 'sinfo' for more information.");
userEnteredCommand = Console.ReadLine();
}
void onCommandLineReturn()
{
userEnteredCommand = Console.ReadLine();
}
// Commands
if (userEnteredCommand == "sinfo")
{
Console.WriteLine(" ");
Console.WriteLine("Program information:");
Console.WriteLine("Created for IAO Corporation, by Zreddx");
Console.WriteLine("This program controls doors, gates and e.t.c within IAO Terratory.");
Console.WriteLine(" ");
Console.WriteLine("This program is protected by copyright, do not redistribute. ");
}
else
{
Console.WriteLine("That command does not exist, do 'programs' for a list of actions.");
}
onCommandLineReturn();
}
}
}
Console applications close when they get to the end of Main. It's exiting after the Console.ReadLine in onCommandLineReturn();.
Add a bool variable called keepLooping, set it to true, and wrap your code in a while(keepLooping) statement. Somewhere in your program flow, check for input like "quit" or "exit" and set the keepLooping variable to false.
Here's an example of it in a dotnetfiddle: https://dotnetfiddle.net/Jguj5k

C#: Using Pin Pad instead of a keyboard for input in a console Application

I am using Visual Studio 2013, .NET 4.5, C#, OS Windows 8.
I want to set a pin pad as only input device for my Console application in C#... Which library will be used...
This guy is doing it but he only writes opendevice() and RequestPin() which not built in functions, whereas I need to know libraries and function. Are there any built in assemblies in .NET for that? Take a little time if possible and explain if anyone knows.... or point me in the right direction for research...
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace DisableOpt
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Password:");
string inputP = Console.ReadLine();
if (inputP == "2517")
{
Console.Clear();
Console.WriteLine("Press 1 to do to 1 ");
Console.WriteLine("Press 2 to do to 2 ");
Console.WriteLine("Press 3 to do to 3 ");
Console.WriteLine("Press 4 to do to 4 ");
Console.WriteLine("Press 9 to exit ");
option = Convert.ToInt32(Console.ReadLine());
switch (option)
{
case 1:
Console.WriteLine("Something");
break;
case 2:
Console.WriteLine("Something");
break;
case 3:
Console.WriteLine("Something");
break;
case 4:
Console.WriteLine("Something");
break;
case 9:
Environment.Exit(0);
break;
default:
Console.WriteLine("Selected Option Not Valid");
break;
}
}
else
{
Console.WriteLine("Wrong Password Application is exiting...");
Thread.Sleep(3000);
Environment.Exit(0);
}
}
}
}

Output message to Console window if command line parameters incorrect

Can anyone help me output a message to a console box when my .exe is called with the wrong parameters?
Yesterday, some very kind people helped me work out how to call my app without a UI
Here is the thread
command line to make winforms run without UI
So, I have told my app to respond to "/silent archive=true transcode=true" and runs without a UI. Great!
Is it possible to output a message to the command window if they get the command incorrect?
as in "Parameters must be specified like this: /silent archive=true transcode=true"
I have tried this but nothing displays in the dos window..
static void Main(string[] args)
{
if (args.Length > 0)
{
if (args[0] == "/silent")
{
bool archive = false;
bool transcode = false;
try
{
if (args[1] == "transcode=true") { transcode = true; };
if (args[2] == "archive=true") { archive = true; };
Citrix_API_Tool.Engine.DownloadFiles(transcode, archive);
}
catch
{
Console.Write ("Hello");
Console.ReadLine();
return;
}
}
}
else
internal sealed class Program
{
[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
private static void Main(string[] args)
{
if(false)//This would be the run-silent check.
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
try
{
throw new Exception("Always throw, as this tests exception handling.");
}
catch(Exception e)
{
if(AttachConsole(ATTACH_PARENT_PROCESS))
{
//Note, we write to Console.Error, not Console.Out
//Partly because that's what error is for.
//Mainly so if our output were being redirected into a file,
//We'd write to console instead of there.
//Likewise, if error is redirected to some logger or something
//That's where we should write.
Console.Error.WriteLine();//Write blank line because of issue described below
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine();//Write blank line because of issue described below
}
else
{
//Failed to attach - not opened from console, or console closed.
//do something else.
}
}
}
}
A problem is that the console would have already returned to taking input from the user. Hence you really want to try to have your exception as fast as you can if you're going to have it, so a fast validation check rather than an exception that might happen down the line, is preferable.

Program won't recognize command line arguments [duplicate]

This question already has an answer here:
Closed 10 years ago.
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.:
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?
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.
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);
}
}
}

Repeat Forever a If Function

I'm developing a simple project, but how I can do a repeat forever a If function(It's like a command-line)? Thanks.
My code is like this:
Console.Write("> ");
var Command = Console.ReadLine();
if (Command == "About") {
Console.WriteLine("This Operational System was build with Cosmos using C#");
Console.WriteLine("Emerald OS v0.01");
}
By any chance do you mean:
while( !(!(!(( (true != false) && (false != true) ) || ( (true == true) || (false == false) )))) == false )
{
Console.Write("> ");
if ("About" == Console.ReadLine())
{
Console.WriteLine("This Operational System was build with Cosmos using C#");
Console.WriteLine("Emerald OS v0.01");
}
}
string Command;
while (true) {
Command = Console.ReadLine();
if (Command == "About") {
Console.WriteLine("This Operational System was build with Cosmos using C#");
Console.WriteLine("Emerald OS v0.01");
}
}
Your question is unclear, but you probably want to do something like this:
while(true) { //Loop forever
string command = Console.ReadLine();
if (command.Equals("Exit", StringComparison.OrdinalIgnoreCase))
break; //Get out of the infinite loop
else if (command.Equals("About", StringComparison.OrdinalIgnoreCase)) {A
Console.WriteLine("This Operational System was build with Cosmos using C#");
Console.WriteLine("Emerald OS v0.01");
}
//...
}
I don't think your question is really clear. But here is an attempt :)
while (true) {
if (i ==j ) {
// whatever
}
}
You mean this?
while(true) {
if( ...) {
}
}
PS: this is one of my favourite preprocessor hacks. Doesn't work in C# though, only C/C++.
#define ever (;;)
for ever {
//do stuff
}
I think you just want a simple while loop with (at least) one exit point.
while(true)
{
Console.Write("> ");
var command = Console.ReadLine();
if (command == "about") {
Console.WriteLine("This Operational System was build with Cosmos using C#");
Console.WriteLine("Emerald OS v0.01");
} else if (command == "exit") {
break; // Exit loop
}
}
You can't use an 'if' statement by itself because when it gets to the end your program will continue executing the next statement in your code. I think what you're after is a 'while' statement that always evaluates to true.
e.g.
string Command;
while(true)
{
Command = Console.ReadLine();
if (Command == "About")
{
Console.WriteLine("This Operational System was build with Cosmos using C#");
Console.WriteLine("Emerald OS v0.01");
}
}
This loop will be inescapable unless an exception is thrown or you execute a break statement (or whatever the equivalent is in C#, I'm a Java guy - don't hate me).

Categories