Em đang làm bài tập thực thi lệnh shell trong command line bằng c#, em làm theo hướng dẫn trên các 4rum và msdn, nhưng khi chạy thì có 1 số lệnh thì chạy được nhưng có 1 số lệnh không chạy, ví dụ như mở vào thư mục nào đó hoặc gõ lệnh exit để thoát thì nó không thoát, code của em như sau:
và khi gõ 1 số lệnh sai thì em không biết làm sao để nó báo là lệnh sai, mong được các anh chi và các bạn giúp.
PHP Code:
static void Main(String[] args)
{
while (true)
{
Console.WriteLine("Nhap lenh: ");
string cmd = Console.ReadLine();
ProcessStartInfo info = new ProcessStartInfo(@"C:\WINDOWS\system32\cmd.exe", "/Q /C " + cmd);
info.CreateNoWindow = false;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardError = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process pro = new Process();
pro.StartInfo = info;
String s = "";
String t = "";
pro.Start();
s = pro.StandardOutput.ReadToEnd();
t = pro.StandardError.ReadToEnd();
if(s!= null)
Console.WriteLine(s);
if (t != null) Console.WriteLine(t);
pro.WaitForExit();
}
}
Comment