5 Replies - 10040 Views - Last Post: 04 February 2010 - 07:28 AM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Running command line commands from WinForm

Posted 03 February 2010 - 06:48 PM

I am taking a class where we use the command line to write Jscript files to be run within windows. But Windows 7 command prompt does not allow for the same commands (we use XP at class). So I was thinking of making a simple editor to be able to create these Jscript files and then run them. However to run them we use the cscript <filename> in the command prompt. How would I say run this command from my new editor?

Kind of like the debug option in Visual Studio but instead it runs the program via commandline.

Is This A Good Question/Topic? 0
  • +

Replies To: Running command line commands from WinForm

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Running command line commands from WinForm

Posted 03 February 2010 - 07:11 PM

View Postblank_program, on 03 February 2010 - 05:48 PM, said:

However to run them we use the cscript <filename> in the command prompt. How would I say run this command from my new editor?


Does this help?
http://msdn.microsof...y/cb20e19t.aspx
Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Re: Running command line commands from WinForm

Posted 03 February 2010 - 07:16 PM

Not really. I need to launch cmd.exe (which I know how to do) but then have it run the command cscript followed by a filename (either picked form an open dialog or the current file).
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Running command line commands from WinForm

Posted 03 February 2010 - 07:56 PM

Just make your main method take arguments:

public static void Main(string[] args)
{

}



Now, it kind of depends on what arguments are being passed. If you just pass the whole string of your command then check if the args > 0 to ensure there is something.

Once you know that, you can launch cmd with the arguments via System.Diagnostics.Process class or you can do something different, however it should look something like this:

public static void Main(string[] args)
{
    if (args.Length == 1)
    { // Do special command line stuff in here }
    else
    {
        // Just launch the UI normally
        Application.Run(new MainForm());
    }
}



Does that make sense? Maybe I'm not fully understanding the problem.
Was This Post Helpful? 0
  • +
  • -

#5 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Re: Running command line commands from WinForm

Posted 03 February 2010 - 08:03 PM

My Windows Form application is going to be a text box the user can type Jscript commands into. But I want it to be able to open the command prompt and run the script.

My class did a temperature conversion program called temperature.js, I want to be able to open the command window and run that file using the cscript command.

I got it to run using this:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace JScriptEdit
{
    public partial class formJMain : Form
    {
        public formJMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Temp = "temperature.js";
            Process.Start("cscript", Temp);
        }
    }
}



but the command window closes and you cannot see the output from the script.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Running command line commands from WinForm

Posted 04 February 2010 - 07:28 AM

So you need to use the cmd.exe to run a file? How about this?

Process.Start("cmd.exe", @"/C ping www.google.com > c:\test\log.log");



The "/C" is required. It tells the Command Prompt to carry out the following command.

Now, if you want to get the output back, you would do this...

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/C ping www.google.com";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1