7

I had look this question about Passing command line arguments in C#.

But in my case I have to pass array of parameters to the calling .exe file.

e.g.

var arr = new string[] {"Item title","New task","22","High Priority"}

Is it possible to use Process.Start() with exe path along with the array

I have the .exe path

const string path = @"C:\Projects\Test\test.exe";

Thanks

Community
  • 1
  • 1
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
  • 2
    Possible duplicate of [Escape command line arguments in c#](http://stackoverflow.com/questions/5510343/escape-command-line-arguments-in-c-sharp) – quetzalcoatl Oct 11 '16 at 18:47
  • 3
    Most answers in this question are wrong, bad, incomplete or just more or less unsafe when the arguments contain special characters like quotes or whitespaces. Please see [this answer](http://stackoverflow.com/a/6040946) – quetzalcoatl Oct 11 '16 at 18:49

3 Answers3

-3

One option is to put the array in one string so it is viewed as one argument by the method. In your method, you can then parse that one argument. Something like:

"Item title, New task, 22, High Priority"

You can use your existing array by doing:

var arrAsOneString = string.Join(", ", arr);

Inside your method, do:

var values = argument.Split(',').Select(x => x.Trim());

I added the trim to do away with spaces.

Peter
  • 13,733
  • 11
  • 75
  • 122
-4

Its not possible to pass array as argument, you can pass a string with Comma Separator:

 ProcessStartInfo info = new ProcessStartInfo();
 info.Arguments = "Item title,New task,22,High Priority"
stuartd
  • 70,509
  • 14
  • 132
  • 163
andy
  • 5,979
  • 2
  • 27
  • 49
-5

Please try this:

        var arr = new string[] {"Item title", "New task", "22", "High Priority"};
        const string path = @"C:\Projects\Test\test.exe";
        const string argsSeparator = " ";
        string args = string.Join(argsSeparator, arr);

        Process.Start(path, args);
petro.sidlovskyy
  • 5,075
  • 1
  • 25
  • 29
  • 11
    And how will he know if space is part of argument or separator? :D – Grzegorz W Sep 06 '12 at 08:35
  • 2
    @GrzegorzWilczura then maybe `string args = string.Join(" ", arr.Select(e => "\"" + e + "\""));` ? – wasyl Sep 06 '12 at 08:37
  • 2
    @GrzegorzWilczura If separator is not space he may use any other separator :) just change a const. Thanks for comment. – petro.sidlovskyy Sep 06 '12 at 08:38
  • 9
    If done "correctly" and working for _arbitrary_ arguments, it is a little more [complicated](http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx). Basically, you have to apply the rules of [CommandLineToArgv](http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx) but in reverse. Also see [this answer](http://stackoverflow.com/a/2611075/21567). – Christian.K Sep 06 '12 at 08:50
  • 1
    This answer is so wrong.. stringizing process arguments in Windows is much more complicated. Please see [this answer](http://stackoverflow.com/a/6040946) – quetzalcoatl Oct 11 '16 at 18:47
  • DON'T EVER do this. Otherwise we'd never get rid of all those sh*tty applications which can not even start from a directory with spaces in path. – ratijas Feb 21 '20 at 10:14