-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.

Smack
  • 936
  • 1
  • 13
  • 31
  • 7
    It's not at *all* clear what's going on here, in terms of ASP.NET and Windows Forms being involved at the same time. Please give a lot more detail. – Jon Skeet Sep 24 '10 at 10:44
  • What are you trying to do with javascript that is causing you to want to run it from a winform? – Tony Abrams Sep 24 '10 at 12:16
  • what does "building a web site" have to do with winforms? How are winforms involved at all!? – Kirk Woll Sep 24 '10 at 14:10
  • you need to provide more detail and context to your question. Validation in WinForms using javascript?? No. Sounds more and more like you're building a WebForms app, in which client-side validation using javascript is commonly used. – Peter Lillevold Sep 27 '10 at 18:27

2 Answers2

1

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.

Community
  • 1
  • 1
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
0

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,420
  • 3
  • 18
  • 34