Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing lengthy strings (as an argument) into a console application

I'm creating a console application in C# to which, at execution, it is passed a bunch of data. Three of them are short strings, such as username, password, etc. However, one of them is a rather lengthy XML document.

How long can the strings be, when passing them in as command-line arguments?

Are there any better alternatives out there for passing in data to a C# console app at execution?

like image 234
meh Avatar asked Jun 17 '12 08:06

meh


People also ask

How do you pass runtime arguments in C#?

How to Pass or Access Command-line Arguments in C#? In C#, the Main() method is an entry point of the Console, Windows, or Web application (. NET Core). It can have a string[] args parameter that can be used to retrieve the arguments passed while running the application.

What are command line arguments C#?

Arguments that are passed by command line known as command line arguments. We can send arguments to the Main method while executing the code. The string args variable contains all the values passed from the command line. In the following example, we are passing command line arguments during execution of program.


2 Answers

Found here following about limitations:

  • The maximum command line length for the CreateProcess function is 32767 characters. This limitation comes from the UNICODE_STRING structure.

  • CreateProcess is the core function for creating processes, so if you are talking directly to Win32, then that's the only limit you have to worry about. But if you are reaching CreateProcess by some other means, then the path you travel through may have other limits.

  • If you are using the CMD.EXE command processor, then you are also subject to the 8192 character command line length limit imposed by CMD.EXE.

  • If you are using the ShellExecute/Ex function, then you become subject to the INTERNET_MAX_URL_LENGTH (around 2048) command line length limit imposed by the ShellExecute/Ex functions. (If you are running on Windows 95, then the limit is only MAX_PATH.)

What better ways or alternative - use file, maybe XML, with all your parameters there, and pass this file as command line argument.

like image 104
Regfor Avatar answered Oct 22 '22 14:10

Regfor


If the XML is your program's principal input, then I strongly suggest that you read it from standard input instead of as a command-line argument (which would seem unusual in this case). This would give users of your program three ways for passing in XML:

  • execute your program, and manually type in some XML
  • use the output of another program: other_program … | program …
  • redirect standard input from a file: program … < input.xml

If the XML, however, is not the main input, I'd offer a command-line switch for specifying an input file name; which gives you one way to specify the XML:

  • specify the input file via parameter: program ... -f input.xml

Apart from this advice, I cannot answer your actual question precisely, but I've noticed in the past that Windows' cmd.exe (IIRC under Windows 2000 or XP) was fairly limited with regard to the maximum length of a command.

like image 41
stakx - no longer contributing Avatar answered Oct 22 '22 13:10

stakx - no longer contributing