i want to execute cmd command in c#
- User
i want to execute cmd command in c#
- Assistant
To execute a CMD command in C#, you can use the
System.Diagnostics.Processclass. This allows you to start and interact with processes. Below is an example of how you can execute a command: - User
a cmd command that never sttop
- Assistant
- User
i do not want use using
- Assistant
If you prefer not to use the
usingstatement, you need to make sure that you manually manage the lifecycle of the object. This means you have to explicitly call on the instance to free up resources once you're done with it. Here’s how you can do it: - User
debug: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace MeetingLobyHandler { public partial class Main : Form { public Main() { InitializeComponent(); }
codepublic string Command = ""; public Process process = new Process(); private void Main_Load(object sender, EventArgs e) { var lines = File.ReadAllLines("config.ini"); Command = lines[0].Split(':')[1]; RunCommand(); timer_command.Interval = Convert.ToInt32(lines[1].Split(':')[1]); timer_command.Enabled = true; } private void timer_command_Tick(object sender, EventArgs e) { RunCommand(); KillCommand(); } public void RunCommand() { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/C " + Command, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = false }; process.StartInfo = processStartInfo; process.Start(); } public void KillCommand() { process.Kill(); } } - Assistant
- User
the cmd windows does not close after kill: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace MeetingLobyHandler { public partial class Main : Form { public Main() { InitializeComponent(); }
codepublic string Command = ""; public Process process = new Process(); private void Main_Load(object sender, EventArgs e) { var lines = File.ReadAllLines("config.ini"); Command = lines[0].Split(':')[1]; RunCommand(); timer_command.Interval = Convert.ToInt32(lines[1].Split(':')[1]); timer_command.Enabled = true; timer_command.Tick += timer_command_Tick; timer_command.Start(); } private void timer_command_Tick(object sender, EventArgs e) { KillCommand(); RunCommand(); } public void RunCommand() { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/C " + Command, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = false }; process.StartInfo = processStartInfo; process.Start(); } public void KillCommand() { process.Kill(); process.Close(); } } - Assistant