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

前端 未结 4 1220
小蘑菇
小蘑菇 2020-12-06 07:45

I want to load a JavaScript file using Jint, but I can\'t seem to figure it out. The documentation said I could do something like engine.run(file1), but it does

相关标签:
4条回答
  • 2020-12-06 08:10

    Personally I use:

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

    Compact and just works.

    0 讨论(0)
  • 2020-12-06 08:13

    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();
    
    0 讨论(0)
  • 2020-12-06 08:20

    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();
    
    0 讨论(0)
  • 2020-12-06 08:29

    Try

     using(FileStream fs = new FileStream("test.js", FileMode.Open))
     {
        JintEngine js = JintEngine.Load(fs);
        object result = js.Run("return status;");
        Console.WriteLine(result);
     }
    
    0 讨论(0)
提交回复
热议问题