在cmd中使用C#运行一系列命令

时间:2016-12-14 14:55:59

标签: c# windows cmd filepath

最近我们一直在使用Berkley Pacman AI课程。我们必须分析alpha,epsilon和gamma的变化值对AI的影响。

要将命令直接插入CMD,我们告诉CMD:

pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7

现在,我想运行一系列测试,我们更改这些给定变量的值。所以我想打开CMD,运行很多命令(基本上是相同的)并将输出保存在文本文件中。我在StackExchange上找到了一些信息,它给了我以下代码:

string command = "pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7";
Process.Start("CMD.exe", command);

尽管它打开了CMD,似乎没有做任何事情。此外,CMD的目录是我的解决方案的目录。 这应该是相当容易的(Altough Windows API可能很难使用)

任何人都可以帮忙,或者给我一个通用的解决方案吗?

2 个答案:

答案 0 :(得分:1)

尝试ProcessStartInfo类。 MSDN ProcessStartInfo

因为这是一个AI课程。我可能会做一个PacmanArgument课程。 PacmanArgument将为每个可能的命令行参数和要调用的自定义ToString方法提供属性。这样可以更容易地以编程方式生成类似遗传算法的参数,假设输出可以被视为适应性。

主要功能:

double MAX_EPSILON = 1; //I assume there are constraints
//create packman agent with initial values
PacmanAgent agent = new PackmanAgent(0,0,0) //epsilon,alpha,gamma
//create Process info 
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "pacman.py"
psi.WorkingDirectory = @"C:/FilePathToPacman"
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

string output;
Console.WriteLine("***** Increasing Eplison Test *****");
while( agent.Epsilon =< MAX_EPSILON )
{
    psi.Arguments = agent.GetArgument();
    // Start the process with the info we specified.
    // Call WaitForExit and then the using-statement will close.
    using (Process process =  Process.Start(psi))
    {
        output = process.StandardOutput.ReadToEnd(); //pipe output to c# variable
        process.WaitForExit(); //wait for pacman to end
    }

    //Do something with test output
    Console.WriteLine("Epsilon: {0}, Alpha: {1}, Gamma: {2}",agent.Epsilon,agent.Alpha,agent.Gamma );
    Console.WriteLine("\t" + output);

    agent.IncrementEpsilon(0.05); //increment by desired value or default(set in IncrementEpsilon function)
}

Pacman Agent类:

public class PacmanAgent
{
    private string ArgumentBase = "-q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a ";
    [Range(0, 1, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    public double Epsilon { get; set; }
    [Range(0, 1, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    public double Alpha { get; set; }
    [Range(0, 1, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    public double Gamma { get; set; }

    public PacmanAgent(int epsilon, int alpha , int gamma )   
    {
         Epsilon = epsilon;
         Alpha = alpha; 
         Gamma = gamma; 
    }   

    public string GetArgument()
    {
        string argument = string.Format("{0} epsilon={1}, alpha={2}, gamma={3}", ArgumentBase, Epsilon, Alpha, Gamma)
        return argument
    }

    public void IncrementEpsilon(double i = 0.01)
    {
        Epsilon += i;
    }
    public void IncrementAlpha(double i = 0.01)
    {
        Alpha += i;
    }
    public void IncrementGamma(double i = 0.01)
    {
        Gamma += i;
    }
}

*我在IDE上写了这个,所以请原谅任何语法错误

答案 1 :(得分:0)

您首先可以构建一个包含所有pacman.py命令的临时批处理文件。使用标准方法在C#中创建文件。使用标准CMD.EXE管道功能附加到文件(下面的myfile.txt)。

您的临时.BAT文件如下:

cd \mydirectory
pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 >> myfile.txt
pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 >> myfile.txt
pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 >> myfile.txt

然后以与您已使用的方法类似的方式运行它:

var command = "mytemp.bat"
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
process = Process.Start(processInfo);
process.WaitForExit();

如果你不想在批处理文件中输入它,你也可以让Process重定向它的标准输出并将其读入C#中的字符串变量。

相关问题