Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a c# Console App with .application file type from a .BAT

I've created a console app (using Visual Studio 2010) which can read command arguments.

When I debug, I parse some test parameters which are set in Project-> [project name] Properties... -> Debug -> Command line arguments:

It reads: "parametername1|parametervalue1" "parametername2|parametervalue2" "parametername3|parametervalue3"

I used the following code to read the parameters:

for (Int16 argumentsCount = 0; argumentsCount < args.Length; argumentsCount++)
{
    String[] parameterItem = args[argumentsCount].Split('|');
    String parameterName = parameterItem[0].ToString();
    String parameterValue = parameterItem[1].ToString();
    /*code continues*/

}

When I run in debug mode the app it works just fine and all parameters are read.

I then published the app to a server and ensured it was installed with the correct permissions (for the purposes of demonstration lets say it's on C:\MyApp and the Complied code resides in MyApp.application

I then created a batch script that executes the app. The *.BAT contains the following command:

"C:\MyApp\MyApp.application" "parametername1|parametervalue1" "parametername2|parametervalue2" "parametername3|parametervalue3"

This kind of works as the application executes when I run the batch... However... none of my parameters are being received by my app. I know this because I recompiled and published with some code to read how many parameters are being received with:

Console.Write("Arguments " + args.Length.ToString());

and that shows Arguments: 0

Can someone please tell me how to write my batch script to run the app and parse my parameters/command line arguments.

like image 708
Fab Avatar asked Nov 14 '22 22:11

Fab


1 Answers

ETA: Nevermind. Your problem is .application instead of a .exe. Look in your file associations what happens with .application compared to .exe:

> assoc .application
.application=Application.Manifest

> ftype Application.Manifest
Application.Manifest=rundll32.exe dfshim.dll,ShOpenVerbApplication %1

> assoc .exe
.exe=exefile

> ftype exefile
exefile="%1" %*

You see the difference in what is passed there? Namely that normal executables get command-line arguments (the %*). So I guess you should use an executable instead of an executable manifest or whatever .application actually is (I've never seen it in the wild, honestly).


With a fairly minimal test program

class Args {
    static void Main(string[] args) {
        for (int i = 0; i < args.Length; i++) {
            System.Console.WriteLine("[{0}]=<{1}>", i, args[i]);
        }
    }
}

it works fine for me. The following batch file:

@"args.exe" "parametername1|parametervalue1" "parametername2|parametervalue2" "parametername3|parametervalue3"

yields the following output:

[0]=<parametername1|parametervalue1>
[1]=<parametername2|parametervalue2>
[2]=<parametername3|parametervalue3>

So I guess there is something wrong in the code you didn't show us. Maybe you're not actually using the command-line arguments in your C# application but instead reference a different string[] there?

like image 161
Joey Avatar answered Feb 03 '23 01:02

Joey