Search
Latest topics
Giao tiếp với cmd trong lập trình C#?
Page 1 of 1
Giao tiếp với cmd trong lập trình C#?
public void ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
}
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
}
Re: Giao tiếp với cmd trong lập trình C#?
- Cách ngắn hơn thì có thể là như thế này:
Visual C# Code:
Lựa chọn code | Ẩn/Hiện code
string commandToExecute = @"md c:\Test";// @"c:\windows\system32\calc.exe";
System.Diagnostics.Process.Start(@"cmd", @"/c " + commandToExecute);
- Còn cách đầy đủ hơn thì bạn tham khảo ở đây:
http://www.digitalcoding.com/Code-Sn...-from-Net.html
Visual C# Code:
Lựa chọn code | Ẩn/Hiện code
string commandToExecute = @"md c:\Test";// @"c:\windows\system32\calc.exe";
System.Diagnostics.Process.Start(@"cmd", @"/c " + commandToExecute);
- Còn cách đầy đủ hơn thì bạn tham khảo ở đây:
http://www.digitalcoding.com/Code-Sn...-from-Net.html
Similar topics
» Im lặng - Một nghệ thuật giao tiếp
» Giáo trình php
» Một vài điều cần lưu ý trong lập trình C#
» Giáo trình Java
» Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP
» Giáo trình php
» Một vài điều cần lưu ý trong lập trình C#
» Giáo trình Java
» Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Thu Aug 23, 2012 5:38 am by Admin
» Tuyệt kỹ cua giai
Thu Aug 23, 2012 5:36 am by Admin
» NETCAT.........
Mon Aug 13, 2012 6:35 am by Admin
» Bảo mật CSDL bằng phương pháp mã hóa.
Tue Apr 17, 2012 10:04 pm by Admin
» Hàm mã hóa MD5 bằng JavaScript
Tue Apr 17, 2012 10:03 pm by Admin
» Giá của món quà
Fri Apr 13, 2012 6:01 am by Admin
» Sẽ chỉ yêu ai?
Fri Apr 13, 2012 6:01 am by Admin
» Cách đọc bảng chữ cái!
Thu Apr 12, 2012 10:37 pm by Admin
» Gắn trojan, keylog, virus vào website, forum
Tue Apr 10, 2012 1:14 am by Admin