问题描述:

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.

网友答案:

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?

网友答案:

The pipe character | has a special meaning in batch files. I would suggest using a different character to make things easier. Otherwise you have to use a Escape Character to use the pipe character. It would probably look like this:

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

note the caret ^ before the pipe |.

相关阅读:
Top