How to call a console app recursively with arguments?
I would like to have a console app that is passed in a arg to be able to call itself passing in the same command arg that was sent in initially.
If I try this however
static void Main(string[] args)
{
Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
string cmd = Environment.CommandLine;
Process again = new Process();
again.StartInfo.FileName = ass.Location;
again.StartInfo.Arguments = args[0];
Console.WriteLine("Running with: " + args[0]);
System.Threading.Thread.Sleep(10000);
again.Start();
return;
}
The initial call 开发者_开发问答print "running with: Argument1" but the second call fails because the args array is empty.
Works for me. I compiled exactly this code:
using System;
using System.Diagnostics;
using System.Reflection;
class Test
{
static void Main(string[] args)
{
Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
string cmd = Environment.CommandLine;
Process again = new Process();
again.StartInfo.FileName = ass.Location;
again.StartInfo.Arguments = args[0];
Console.WriteLine("Running with: " + args[0]);
System.Threading.Thread.Sleep(1000);
again.Start();
return;
}
}
Using the command line:
csc Test.cs
And then ran it as:
Test.exe hello
... and it started extra processes recursively, each of which printed "Running with: hello".
精彩评论