I have a C# project that takes in arguments that I'm trying to run from a bat file. For an application that doesn't take arguments I just put the following inside a file called run.bat
pathname\helloworld\bin\Debug\helloworld.exe
What if my program takes in parameters, how do I adjust. When is the echo of used? any good tutorial on writing batch files? Thanks
pathname\helloworld\bin\Debug\helloworld.exe "argument 1" "argument 2" 3
using System;
public class Demo {
public static void Main(string[] args) {
foreach(string arg in args)
Console.WriteLine(arg);
}
}
I would try
#rem turn off echo - atsign is line-level way how to do it
#echo off
#rem provided your app takes three params, this is how to pass them to exe file
pathname\helloworld\bin\Debug\helloworld.exe %1 %2 %3
String arguments tend to just follow the EXE after a space. So if you have two parameters "Bob", and "is a jerk" you could write this in the .bat:
helloworld.exe Bob "is a jerk"
Bob becomes the first parameter, since it has whitespace around it. But "is a jerk" is all one because of the quotation marks. So this would be two parameters.
Your tags mention C, but I'm unclear if you actually meant you're calling this from C, the completely seperate language; you seem to be just indicating you use a batch file.
For your bat file, just add parameters after your exe path, like this:
pathname\helloworld\bin\debug\helloworld.exe param1 param2
Then, there's a method in your Program.cs file that looks like this:
[STAThread]
static void Main(string[] args)
{
Application.Run(args.Length > 0 ? new Main(args[0]) : new Main());
}
Here you can tweak the parameters that are processed and send them to your startup form.
As for the echo, that's just like a print statement, anything you want to output to the console window...
Related
I'm trying to create a C# console app to do some proccess. I want to open my demo.exe and send some parameters from a .BAT file to that cosole.
I know the .bat should be something like:
demo.exe -a cclock -cc 1306 -mc 1750
But, I don't have any idea to make my .exe to get the parameters I'm sending.
This is where the arguments to Main method helps.
In an standard C# program entry method is like,
static int Main(string[] args)
Here args[] is the array of arguments passed to your executable via command line.
So in your example,
demo.exe -a cclock -cc 1306 -mc 1750
args is a string array containing following,
{"-a", "cclock", "-cc", "1306", "-mc", "1750"}
You can retrieve these value in this manner,
args[0] = "-a"
args[1] = "cclock"
args[2]= "-cc" ...... and so on
You can use these value for the rest of your code.
Remember that whatever values you pass are broken into separate string values on each occurrence of white-space. Also whatever value you pass will be taken as string. So you have to do your own validation and parsing.
Your application's Main method (typically in Program.cs) can accept a parameter string[] args, which you can access to get the command line parameters used to launch your app. Alternatively, you can also use Environment.GetCommandLineArgs() anywhere in the application to do the same thing.
I'm creating a small commandline tool for a customer and trying to verify its output. The output is been written to the commandline itself. Since its rather a huge file that's been used as an input file and thus a huge out to write to the commandline, I wanted the output to redirect to a file.
Usually I use use commandline arguments like so to redirect the output to a file:
a.exe ./input.txt > ./ouput.txt
However, in my program, I try to verify the input:
static void Main(string[] args)
{
if (args.Length != 1)
throw new ArgumentException();
...
And args now is:
args[0] = ./input.txt
args[1] = >
args[2] = ./ouput.txt
Honestly I personally still expect only one argument, since the file is been created and thus the shell does understand what I mean. So... what am I doing wrong? Should I use args or something else?
Thank you in advance!
Are you passing the arguments through Visual Studio? It will only work if you untick Enable the Visual Studio hosting process.
Image and explanation from here.
Hi I would like to make my self able to maybe define my app into CMD by that I can type program instead of program.exe kind of like how ping works for example.
I also need help with arguments.
The point of my app is to send a get request to a local server evaling PHP fetching a result from it so I can easy quick debug things and calculate things ect from CMD.
So for example I have to do.
W:\Users\example>e.exe
echo "example";
.....
example
W:\Users\example>
Tow things with the above are very annoying. I need to enter e.exe and THAN I need to enter the code to eval :-/
How could I make it so I could just do
W:\Users\example>e echo "example";
.....
example
W:\Users\example
I really would like to get this working to make use faster + more simple ! this is a programmers way to calculating math :P
EDIT:
Below is the code;
static void Main(string[] args)
{
WebClient client = new WebClient();
Console.WriteLine("...");
string input = Console.ReadLine();
string php = client.DownloadString("http://192.168.1.50/test.php?exec="+input);
Console.WriteLine(".....");
Console.WriteLine("");
Console.WriteLine(php);
Console.WriteLine("");
}
I've tried to do "+arg[0] but does nothing at start :?
Don't type ".exe" as you don't need to type it... You may want to put your e.exe some place listed in PATH environment variable (or add path to the executable to the PATH).
Main(string[] args) are arguments passed to your program - use them whatever way you want. Note that they will be split on spaces, so you may need to String.Join them back if you need all arguments as one.
You can also use Environment.CommandLine if you need access to comple not parsed command line.
In this article http://nagios.sourceforge.net/docs/3_0/eventhandlers.html#example they show how to get arguments from nagios to shell script. I have done this. Now in this article they show how to receive nagios parameters in .bat files. You can access nagios parameters like so:
(::echo 1: %1 2: %2 3: %3 4: %4)
In shell script I would access the variables like this: echo $1, echo $2 ect...
How do I access this variables in c# in .exe file?
It looks like Nagios just passes the arguments via command line. Assuming your C# project is just a console application, the command line arguments will be passed to Main. So the following will print 3 arguments:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]); // echo $1
Console.WriteLine(args[1]); // echo $2
Console.WriteLine(args[2]); // echo $3
}
}
Note, before retrieving the command line arguments make sure to check that the indices are valid.
I am trying to read command lien argument but it seems there is some kind of bug in .NET.
The parameter which I pass to my console application
/i "C:\Projects\PC\trunk\Simulator\PDF-Source\PDF-Source\bin\Debug\ConversionFiles\dummy.pdf" /o "result"
CommandLine variable return three arguments, but I pass four and values of these arguments messed up.
/i = true
"C:\Projects\PC\trunk\Simulator\PDF-Source\PDF-Source\bin\Debug\ConversionFiles\dummy.pdf" = true
/o = "result"
As you see only the last argument is parsed corectlly. Is this bug?
regards,
Tomas
My code
static void Main(string[] args)
{
Arguments CommandLine = new Arguments(args);
It looks like the problem is in the Arguments class. I bet if you check out args array you will find 4 elements there.