PDA

View Full Version : سوال: نحوه ی اجرا دستورات cmd



parsa lotfy
شنبه 26 فروردین 1391, 18:45 عصر
دوستان در c++برای اجرا دستور cmdمینویسیم
system ("دستور");
اینجا باید چیکار کنیم؟

mostafah110
شنبه 26 فروردین 1391, 21:39 عصر
سلام... منم به اجرای دستورات cmdاحتیاج پیدا کردم،بعد از سرچ توی سایت های مختلف این تابع رو نوشتم، امیدوارم به دردتون بخوره:


public void ExecuteCommand(string 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.exe", "/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;
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();
}
catch (Exception objException)
{
MessageBox.Show("مشکلی در اجرای فرمان به وجود امد" + "\n" + "مشکل به وجود آمده: " + "\n" + objException.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}



دستورت رو به این تابع می دی و برات اجراش می کنه.