views:

63

answers:

2

i have the js file how can i add that in winform C#, i have developed the front-end and i want to run the js file on button click. i will be thankful to u if u provide the snippet!!!!! I want to run javascript code in windows form thank you.

A: 

You can use cscript.exe to execute JScript on windows & can capture the output from the stdout.

string output;
using (Process cscript = new Process())
{
    // prepare the process
    cscript.StartInfo.UseShellExecute = false;
    cscript.StartInfo.CreateNoWindow = true;
    cscript.StartInfo.RedirectStandardInput = false;
    // RedirectStandardOutput should be True to get output using the StandardOutput property
    cscript.StartInfo.RedirectStandardOutput = true;
    cscript.StartInfo.FileName = "cscript.exe";
    cscript.StartInfo.Arguments = "<Path to your .js file>";

    cscript.Start();
    output = cscript.StandardOutput.ReadToEnd();
    cscript.Close();
}

Obviously your JScript cannot contain any statement that is illegal on windows

for example when you run JS in a browser it contains the window object. There would be no window object when you run JS in shell using cscript.exe

Zuhaib
+1  A: 

As others have stated here, you should clarify your scenario. That said, this question have an answer to how to run javascript from a .Net application.

Peter Lillevold