0
خطای System.NullReferenceException در اجرای دستور سی شارپ
سلام دوستان . .من میخوام با استفاده از سی شارپ دو تا دستور رو تو cmd اجرا کنم . .
string[] cm = new string[2]; cm[0] = "/c net start w32time"; cm[1] = "/c w32tm /resync"; Process[] process = new Process[2]; ProcessStartInfo[] startInfo = new ProcessStartInfo[2]; for(int i = 0; i < 2; i++) { startInfo[i].UseShellExecute = false; startInfo[i].RedirectStandardOutput = true; startInfo[i].Arguments = cm[i]; startInfo[i].FileName = "cmd.exe"; process[i].StartInfo = startInfo[1]; process[i].Start(); string output = process[i].StandardOutput.ReadToEnd(); Console.Write(output); process[i].WaitForExit(); }
ولی موقع اجرا وقتی به اولین خط حقه میرسه این ارور رو میده :
An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication1.exe Additional information: Object reference not set to an instance of an object.
مشکل از کجاست ؟!
1 پاسخ
0
با کد زیر مشکلم حل شد :)
string[] cm = new string[2]; cm[0] = "/c net start w32time"; cm[1] = "/c w32tm /resync"; Process[] process = new Process[2]; ProcessStartInfo[] startInfo = new ProcessStartInfo[2]; for(int i = 0; i < 2; i++) { process[i] = new Process(); startInfo[i] = new ProcessStartInfo(); startInfo[i].UseShellExecute = false; startInfo[i].RedirectStandardOutput = true; startInfo[i].Arguments = cm[i]; startInfo[i].FileName = "cmd.exe"; process[i].StartInfo = startInfo[i]; process[i].Start(); string output = process[i].StandardOutput.ReadToEnd(); Console.Write(output); process[i].WaitForExit(); }