How to load javascript file using jint in c #?

I want to load a JavaScript file using Jint, but I cannot figure it out. The documentation said that I can do something like engine.run(file1) , but it seems the files are not loading. Do I need to do something with the file name?

Here is my JavaScript file:

 // test.js status = "test"; 

Here is my c #

 JintEngine js = new JintEngine(); js.Run("test.js"); object result = js.Run("return status;"); Console.WriteLine(result); Console.ReadKey(); 

If I put the code manually in Run , it will work.

 object result = js.Run("return 2 * 21;"); // prints 42 
+6
source share
4 answers

I found a workaround by manually loading the file into text, instead of loading it by name, as indicated 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(); 
+3
source

Personally, I use:

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

Compact and just works.

+2
source

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
source

Even more concise (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
source

Source: https://habr.com/ru/post/895031/


All Articles