How do I load a JavaScript file with Jint in C#?

天大地大妈咪最大 提交于 2019-11-27 23:13:55

I found a workaround by manually loading the file into text myself, instead of loading it by name as mentioned here: http://jint.codeplex.com/discussions/265939

        StreamReader streamReader = new StreamReader("test.js");
        string script = streamReader.ReadToEnd();
        streamReader.Close();

        JintEngine js = new JintEngine();

        js.Run(script);
        object result = js.Run("return status;");
        Console.WriteLine(result);
        Console.ReadKey();

Personally I use:

js.Run(new StreamReader("test.js"));

Compact and just works.

Try

 using(FileStream fs = new FileStream("test.js", FileMode.Open))
 {
    JintEngine js = JintEngine.Load(fs);
    object result = js.Run("return status;");
    Console.WriteLine(result);
 }

Even more succinctly (using your code)

JintEngine js = new JintEngine();
string jstr = System.IO.File.ReadAllText(test.js);
js.Run(jstr);
object result = js.Run("return status;");
Console.WriteLine(result);
Console.ReadKey();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!