PDA

View Full Version : اجرای cmd در پس زمینه برنامه ؟



bazikadeh
یک شنبه 03 شهریور 1392, 16:01 عصر
من یه جای برنامه نیاز دارم که کدی رو درون cmd اجرا وبخونمش میشه و چطوری ؟

hamid_0341
یک شنبه 03 شهریور 1392, 16:11 عصر
CMD

System.Diagnostics.Process.Start("cmd");

SendKeys.Send("D:");
SendKeys.Send("{enter}");

aliagamon
یک شنبه 03 شهریور 1392, 19:59 عصر
بهتر از روش بالا اینه:

Process.Start("cmd","dir");

در نمونه بالا منظورم از dir همون کامنده من نمونه کامند dir رو نوشتم....

مهرداد صفا
یک شنبه 03 شهریور 1392, 20:13 عصر
با سلام.
یک ProcessStartInfo بسازید و خصوصیت CreateNoWindow را برابر با true و خصوصیت UseShellExecute را برابر false کنید. حتی می توانید مقدار برگشتی را هم کنترل کنید.
مثال:

string result = "";
System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName ="SystemInfo",

WorkingDirectory = @"C:\windows\system32",
CreateNoWindow = true,
UseShellExecute=false,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
Process MyProcess = new Process();
MyProcess.StartInfo = startInfo;
MyProcess.Start();

MessageBox.Show(MyProcess.StandardOutput.ReadToEnd ());

bazikadeh
دوشنبه 04 شهریور 1392, 19:35 عصر
خوب مثلا من میخوام بزنم ping yahoo.com بعد آیپی که بهم بده رو اعلام کنم توی برنامه

cpppro
دوشنبه 04 شهریور 1392, 20:14 عصر
سلام
همین کد بالا رو داخل که دوستمون زحمت کشیدن داخل یه تابع بزار و از طریق یک نخ مجزا تو پس زمینه اجراش کن.

aliagamon
دوشنبه 04 شهریور 1392, 20:33 عصر
با استفاده از کد بالا:

System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd",
Arguments = "/c ping yahoo.com",

CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
Process MyProcess = new Process();
MyProcess.StartInfo = startInfo;
MyProcess.Start();

textBox1.Text= MyProcess.StandardOutput.ReadToEnd();
int startindex = textBox1.Text.IndexOf('[') + 1, leg = (textBox1.Text.IndexOf(']') ) - startindex;
textBox1.Text= textBox1.Text.Substring(startindex, leg);