1.参考来自链接 https://www.xuebuyuan.com/513011.html 与 https://bbs.csdn.net/topics/380229790
这个库文件Interop.MSScriptControl.dll我硬是在我的电脑中没找到,网络上也搜索了,有下载链接,但还是没下载,
有知道出处的可以给我留个言,了解下.
2.后参照了这两个链接 C#中MSScriptControl.ScriptControl组件用法示例_DXCyber409的博客-CSDN博客_c# msscriptcontrol与用MSScriptControl 在 C# winform中执行 javascript js代码并实现传递参数_blacksource的博客-CSDN博客 在项目中添加引用了MSScript.ocx.
具体添加引用方式,【项目】->【添加引用】->【COM】->选择Microsoft Script Control 1.0组件
3.确定后就添加引用完成了,下面就可以进行编写执行脚本代码例子了,我这里仅编写了vbscript的例子,javascript的例子请自行 参考下面代码进行编写.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSScriptControl;//这里添加引用MSScript.ocx成功后,就可以using了
namespace Execute_script
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//调用库函数(ExecuteStatement)
private void button1_Click(object sender, EventArgs e)
{
ScriptControl msc = new ScriptControl();
msc.Language = "VBScript";
msc.ExecuteStatement("msgbox \"hello world\"");
}
//执行表达式(Eval)
private void button2_Click(object sender, EventArgs e)
{
ScriptControl msc = new ScriptControl();
msc.Language = "VBScript";
object l_obj = msc.Eval("1 + 2");
MessageBox.Show(l_obj.ToString());
}
//调用自定义方法sub(method_1,无参数)
private void button3_Click(object sender, EventArgs e)
{
ScriptControl msc = new ScriptControl();
msc.Language = "VBScript";
StringBuilder l_str = new StringBuilder();
l_str.Append("sub method_1()");
l_str.AppendLine("\r\n");
l_str.Append("MsgBox \"call method_1\",vbInformation, \"method_1\" ");
l_str.AppendLine("\r\n");
l_str.Append("end sub");
msc.AddCode(l_str.ToString());
object[] l_args = new object[0];
msc.Run("method_1", l_args);
}
//调用自定义方法sub(method_1,有参数)
private void button4_Click(object sender, EventArgs e)
{
ScriptControl msc = new ScriptControl();
msc.Language = "VBScript";
StringBuilder l_str = new StringBuilder();
l_str.Append("sub method_1(var1, var2)");
l_str.AppendLine("\r\n");
l_str.Append("MsgBox \"call method_1, parameter var1 = \" & var1 & \", parameter var2 = \" & var2,vbInformation, \"method_1\"");
l_str.AppendLine("\r\n");
l_str.Append("end sub");
msc.AddCode(l_str.ToString());
object[] l_args = new object[2];
l_args[0] = "01";
l_args[1] = "02";
msc.Run("method_1", l_args);
}
//调用自定义函数function(func_1,有参数)
private void button5_Click(object sender, EventArgs e)
{
ScriptControl msc = new ScriptControl();
msc.Language = "VBScript"