到微软的网站上下载windows script control,它是一个activex(r) 控件,所以在.net中使用我interop了一下。下载安装完成后,新建一个c#的windows应用程序项目,在解决方案资源管理器中选中引用节点,右键点击选择添加引用菜单,弹出添加引用对话框,单击浏览找到安装windows script control的目录,选取msscript.ocx文件确定。那么在引用节点下会增加一个msscriptcontrol组件,下面是他interop后的所有对象。
using system;
using msscriptcontrol;
using system.text;
namespace zz
{
/// <summary>
/// 脚本类型
/// </summary>
public enum scriptlanguage
{
/// <summary>
/// jscript脚本语言
/// </summary>
jscript,
/// <summary>
/// vbscript脚本语言
/// </summary>
vbscript,
/// <summary>
/// javascript脚本语言
/// </summary>
javascript
}
/// <summary>
/// 脚本运行错误代理
/// </summary>
public delegate void runerrorhandler();
/// <summary>
/// 脚本运行超时代理
/// </summary>
public delegate void runtimeouthandler();
/// <summary>
/// scriptengine类
/// </summary>
public class scriptengine
{
private scriptcontrol msc;
//定义脚本运行错误事件
public event runerrorhandler runerror;
//定义脚本运行超时事件
public event runtimeouthandler runtimeout;
/// <summary>
///构造函数
/// </summary>
public scriptengine():this(scriptlanguage.vbscript)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="language">脚本类型</param>
public scriptengine(scriptlanguage language)
{
this.msc = new scriptcontrolclass();
this.msc.usesafesubset = true;
this.msc.language = language.tostring();
((dscriptcontrolsource_event)this.msc).error += new dscriptcontrolsource_erroreventhandler(scriptengine_error);
((dscriptcontrolsource_event)this.msc).timeout += new dscriptcontrolsource_timeouteventhandler(scriptengine_timeout);
}
/// <summary>
/// 运行eval方法
/// </summary>
/// <param name="expression">表达式</param>
/// <param name="codebody">函数体</param>
/// <returns>返回值object</returns>
public object eval(string expression,string codebody)
{
msc.addcode(codebody);
return msc.eval(expression);
}
/// <summary>
/// 运行eval方法
/// </summary>
/// <param name="language">脚本语言</param>
/// <param name="expression">表达式</param>
/// <param name="codebody">函数体</param>
/// <returns>返回值object</returns>
public object eval(scriptlanguage language,string expression,string codebody)
{
if(this.language != language)
this.language = language;
return eval(expression,codebody);
}
/// <summary>
/// 运行run方法
/// </summary>
/// <param name="mainfunctionname">入口函数名称</param>
/// <param name="parameters">参数</param>
/// <param name="codebody">函数体</param>
/// <returns>返回值object</returns>
public object run(string mainfunctionname,object[] parameters,string codebody)
{
this.msc.addcode(codebody);
return msc.run(mainfunctionname,ref parameters);
}
/// <summary>
/// 运行run方法
/// </summary>
/// <param name="language">脚本语言</param>
/// <param name="mainfunctionname">入口函数名称</param>
/// <param name="parameters">参数</param>
/// <param name="codebody">函数体</param>
/// <returns>返回值object</returns>
public object run(scriptlanguage language,string mainfunctionname,object[] parameters,string codebody)
{
if(this.language != language)
this.language = language;
return run(mainfunctionname,parameters,codebody);
}
/// <summary>
/// 放弃所有已经添加到 scriptcontrol 中的 script 代码和对象
/// </summary>
public void reset()
{
this.msc.reset();
}
/// <summary>
/// 获取或设置脚本语言
/// </summary>
public scriptlanguage language
{
get{return (scriptlanguage)enum.parse(typeof(scriptlanguage),this.msc.language,false);}
set{this.msc.language = value.tostring();}
}
/// <summary>
/// 获取或设置脚本执行时间,单位为毫秒
/// </summary>
public int timeout
{
get{return this.msc.timeout;}
set{this.msc.timeout = value;}
}
/// <summary>
/// 设置是否显示用户界面元素
/// </summary>
public bool allowui
{
get{return this.msc.allowui;}
set{this.msc.allowui = value;}
}
/// <summary>
/// 宿主应用程序是否有保密性要求
/// </summary>
public bool usesafesubset
{
get{return this.msc.usesafesubset;}
set{this.msc.usesafesubset = true;}
}
/// <summary>
/// runerror事件激发
/// </summary>
private void onerror()
{
if(runerror!=null)
runerror();
}
/// <summary>
/// ontimeout事件激发
/// </summary>
private void ontimeout()
{
if(runtimeout!=null)
runtimeout();
}
private void scriptengine_error()
{
onerror();
}
private void scriptengine_timeout()
{
ontimeout();
}
}
}
上面的包装定义了一个scriptlanguage枚举,这样操作起来更方便一点。另外脚本引擎包括了error事件和timeout事件,根据实际使用情况可进行注册。
二.脚本引擎演示
我建了个窗体程序,测试包括脚本语言的选择,是否开启allowui属性,超时时间的设置,以及脚本引擎调用方法的选择。测试程序代码比较长,下面列出了主要部分:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace zz
{
public class form1 : system.windows.forms.form
{
private scriptengine scriptengine;
private system.windows.forms.checkbox checkboxallowui;
private system.windows.forms.textbox textboxresult;
private system.windows.forms.numericupdown numericupdowntimeout;
private system.windows.forms.textbox textboxcodebody;
private system.windows.forms.button buttonrun;
private system.windows.forms.button buttoncancel;
private system.windows.forms.combobox comboboxscript;
private system.windows.forms.textbox textboxparams;
private system.windows.forms.radiobutton radiobuttoneval;
private system.windows.forms.radiobutton radiobuttonrun;
private system.windows.forms.textbox textboxmethodname;
private system.componentmodel.container components = null;
public form1()
{
initializecomponent();
this.comboboxscript.selectedindex = 0;
this.scriptengine = new scriptengine();
this.scriptengine.usesafesubset = true;
this.scriptengine.runerror += new runerrorhandler(scriptengine_runerror);
this.scriptengine.runtimeout += new runtimeouthandler(scriptengine_runtimeout);
}
protected override void dispose( bool disposing )
{
if( disposing )
if (components != null)
components.dispose();
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
private void initializecomponent()
{
//省略
}
#endregion
[stathread]
static void main()
{
application.run(new form1());
}
//运行脚本
private void buttonrun_click(object sender, system.eventargs e)
{
this.scriptengine.reset();
this.scriptengine.language = (scriptlanguage)enum.parse(typeof(scriptlanguage),this.comboboxscript.selecteditem.tostring());
this.scriptengine.timeout = (int)this.numericupdowntimeout.value;
this.scriptengine.allowui = this.checkboxallowui.checked;
if(this.radiobuttoneval.checked)//执行eval方法
{
this.textboxresult.text = this.scriptengine.eval(this.textboxmethodname.text+"("+this.textboxparams.text+")",this.textboxcodebody.text).tostring();
}
else//执行run方法
{
string[] parameters = (string[])this.textboxparams.text.split(',');
object [] paramarray = new object[parameters.length];
for(int i = 0;i<parameters.length;i++)
paramarray[i] = int32.parse(parameters[i]);
this.textboxresult.text = this.scriptengine.run(this.textboxmethodname.text,paramarray,this.textboxcodebody.text).tostring();
}
}
//退出程序
private void buttoncancel_click(object sender, system.eventargs e)
{
this.close();
}
//错误函数
private void scriptengine_runerror()
{
messagebox.show("runerror执行脚本错误!");
}
private void scriptengine_runtimeout()
{
messagebox.show("runtimeout执行脚本超时,引发错误!");
}
}
}
下面是测试程序运行界面:
在文本框中写了一个javascript的函数。输入12,输出12000012。
如果把超时时间调整为1毫秒,那么执行该脚本就会跳出下面的超时提醒框,同时激发事件。
总结,上面演示了javascript脚本,如果有兴趣读者可以写一些vbsript函数进行测试,脚本语言也只列出了三种,看了帮助,他还支持其他一些脚本,如果需要可以添加。另外,因为是调用com,有些返回值是obejct类型的,需要进行转换。在csdn的技术论坛c#板块下时常有朋友问这方面的问题,对于碰到这类问题的朋友,希望通过这篇文章能获得一些你需要的帮助,很高兴能和搞.net的朋友进行交流,我的邮件地址[email protected]