利用C#怎么对JavaScript进行调用-创新互联
这篇文章给大家介绍利用C#怎么对JavaScript进行调用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

cmd调用phantomjs
手动执行
从官方下载phantomjs.exe,拷贝它与要执行的js同目录
打开cmd,输入命令行(参考官方资料的命令行)
phantomjs XX.js 参数1 参数2
获得结果
使用C#执行
//注意:保证phantomjs.exe和js在生成目录下存在
string url = "传参";
//这里调用cmd.exe
Process pProcess = new Process();
//调用phantomjs.exe
pProcess.StartInfo.FileName = $"phantomjs.exe所在路径(可以是相对路径)";
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.UseShellExecute = false;
pProcess.EnableRaisingEvents = false;
//在phantomjs.exe里面执行的命令
pProcess.StartInfo.Arguments = $"Test2.js所在路径(可以是相对路径) {url}";
pProcess.Start();
char[] spliter = { '\r' };
StreamReader sReader = pProcess.StandardOutput;
string[] output = sReader.ReadToEnd().Split(spliter);
foreach (string s in output)
Console.WriteLine(s);
pProcess.WaitForExit();
//取出计算结果
Console.WriteLine(output[0]);
pProcess.Close();
JS如下:
function Test() {
//创建phantomjs对象
var system = require('system');
//取出参数
var data = system.args[1];
console.log(Math.floor(data));
}
Test();
phantom.exit();C#调用JS库
//引用:Jint
string filePath = $"{Environment.CurrentDirectory}//ExcuteJs//TestJs.js";
string data1 = "1";
string data2 = "2";
string jsCode = System.IO.File.ReadAllText(filePath);
var square = new Engine()
.SetValue("data1", data1) // define a new variable
.SetValue("data2", data2) // define a new variable
.Execute(jsCode) // execute a statement
.GetCompletionValue() // get the latest statement completion value
.ToObject(); // converts the value to .NET关于利用C#怎么对JavaScript进行调用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
文章题目:利用C#怎么对JavaScript进行调用-创新互联
文章起源:http://www.jxjierui.cn/article/jdejg.html


咨询
建站咨询
