本人工作需要将一些别人已经发布的软件等 加入自己的算法,然后发布到网上供其他人使用,大多数其它人的程序都是已经成型的exe文件,这就需要在网页中去调用这些程序。R语言当然可以在C#中直接调用,我这里写的是如何运行已有的R脚本,当然机器上必须先安装R软件。
首先要更改 应用池(application Pool)权限, 在IIS中 找到应用池 然后点属性 在身份(Identity)页中改为"Local System"。
具体方法是用API接口:
首先添加包
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
public class runOutFile
{
//先声明
[DllImport("shell32.dll ")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile,
StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
//运行exe文件
public void runTheExe()
{
string Filename ="exe文件名"
string Pathname="文件所在目录"
string args="所需参数"
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(Filename), new StringBuilder(args)或null,
new StringBuilder(Pathname), 1);
}
//运行R脚本 R语言是一门应用广泛的统计工具 大致同上
public void runR()
{
string Filename ="Rscript.exe" //R的脚本运行器
string Pathname=@"C:/Program Files/R/R-2.12.0/bin" //R的安装目录
string args=@"D:/myR.R" //所需运行的脚本
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(Filename), new StringBuilder(args),
new StringBuilder(Pathname), 1);
}