commandline argument parameter limitation

前端 未结 8 1682
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 09:39

Language: C# I have to pass a huge string array (built dynamically) as an argument to run an exe. I am thinking of acheiving it by the below 2 ways. But I am not feeling confid

相关标签:
8条回答
  • 2021-02-08 10:11

    You could store the arguments in a text file and pass that text file as the argument. Your application can then parse the text file to analyse the arguments.

    0 讨论(0)
  • 2021-02-08 10:12

    You may want to consider creating a parameter file and passing the file as the parameter.

    I found this:

    For OS: maximum command line lenght is 32767 characters (this limit is from unicode string structure), command prompt maximum lenght is 8192 characters (this limit is from cmd.exe). You may also check:

    http://support.microsoft.com/kb/830473

    Hope this help.

    0 讨论(0)
  • 2021-02-08 10:18

    Although a bad idea, Process.start with useshellexecute=false would invoke createprocess() which allows for 32767 characters in the command line (although this is also the maximum size for the entire environment block)

    0 讨论(0)
  • 2021-02-08 10:28

    It is not really good practice to use command line arguments for huge arrays. Put your arguments in a configuration file instead, and just pass the filename as a command line argument.

    The OS limit varies with the Windows version. It could be about 2k or 8k:

    http://support.microsoft.com/kb/830473

    0 讨论(0)
  • 2021-02-08 10:28

    If starting the child process directly from the parent process is acceptable (UseShellExecute= false), then you could redirect the StandardInput of the child process and pass arbitrary size of data throw it. Here is an example passing an array of 100000 strings and other stuff, serializing them in binary format.

    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            var exeFilePath = Assembly.GetExecutingAssembly().Location;
            var psi = new ProcessStartInfo(exeFilePath, "CHILD");
            psi.UseShellExecute = false;
            psi.RedirectStandardInput = true;
            Console.WriteLine("Parent - Starting child process");
            var childProcess = Process.Start(psi);
            var bf = new BinaryFormatter();
            object[] data = Enumerable.Range(1, 100000)
                .Select(i => (object)$"String-{i}")
                .Append(13)
                .Append(DateTime.Now)
                .Append(new DataTable("Customers"))
                .ToArray();
            Console.WriteLine("Parent - Sending data");
            bf.Serialize(childProcess.StandardInput.BaseStream, data);
            Console.WriteLine("Parent - WaitForExit");
            childProcess.WaitForExit();
            Console.WriteLine("Parent - Closing");
        }
        else
        {
            Console.WriteLine("Child - Started");
            var bf = new BinaryFormatter();
            Console.WriteLine("Child - Reading data");
            var data = (object[])bf.Deserialize(Console.OpenStandardInput());
            Console.WriteLine($"Child - Data.Length: {data.Length}");
            Console.WriteLine("Child - Closing");
        }
    }
    

    Output:

    Parent - Starting child process
    Child - Started
    Child - Reading data
    Parent - Sending data
    Parent - WaitForExit
    Child - Data.Length: 100003
    Child - Closing
    Parent - Closing

    This example executes in 6 sec in my machine.

    0 讨论(0)
  • 2021-02-08 10:29

    If you are passing 10,000 arguments to a program, you should be putting those arguments in a file and reading the file from disk.

    0 讨论(0)
提交回复
热议问题