您的位置:首页 > 编程语言 > C#

C#通过调用系统命令执行R脚本

2015-08-22 16:42 686 查看
在C#中调用命令执行R脚本,要实现这个功能我们得知道:(1)如何用c#调用系统命令;(2)如何用系统命令执行R脚本;(3)如何写R脚本

一、C#调用系统命令

这里的“startInfo.FileName = "cmd.exe";”是通过C#调用的系统命令,当然你也可以执行cmd.exe,也可以执行其他的应用和程序,比如计算器“calc”,R程序“R.exe”等。这里的“string[] command”参数是要执行的命令语句,我这里是针对的是控制台,因为其他的应用程序我不知道怎么将参数传入程序中。

private void buttonOK_Click(object sender, EventArgs e)
{
try
{
if (workPath == "" || roiPath == "" || rasterPath == "" || textBoxX1.Text == "" || textBoxX2.Text == "")
return;
rowCount = Convert.ToInt32(textBoxX1.Text);
columnCount = Convert.ToInt32(textBoxX2.Text);
bandCount = Convert.ToInt32(integerInput1.Value);
RandomForestClassfier randomForest = new RandomForestClassfier();
randomForest.CreateRandomForestScript(rowCount, columnCount, bandCount, workPath, roiPath, rasterPath);
string[] command = new string[2];
command[0] = "cd /d C:\\Program Files\\R\\R-3.2.1\\bin\\x64";
command[1] = "r CMD BATCH "+workPath+@"\randomForest.r";
randomForest.RunRandomForestScript(command);
MessageBox.Show("影像分类完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


调用系统命令执行脚本
五、 参考资料

1.http://www.jb51.net/article/57477.htm

2.http://www.jb51.net/article/26993.htm

3.http://q.cnblogs.com/q/44086/

4.http://blog.csdn.net/diyiziran/article/details/21379181

5.http://book.2cto.com/201305/21969.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: