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.
Running command line commands from WinForm
Page 1 of 15 Replies - 6805 Views - Last Post: 04 February 2010 - 07:28 AM
Replies To: Running command line commands from WinForm
#2
Re: Running command line commands from WinForm
Posted 03 February 2010 - 07:11 PM
blank_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
#3
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).
#4
Re: Running command line commands from WinForm
Posted 03 February 2010 - 07:56 PM
Just make your main method take arguments:
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:
Does that make sense? Maybe I'm not fully understanding the problem.
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.
#5
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:
but the command window closes and you cannot see the output from 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.
#6
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?
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.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();
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|