Dictionary<string, DeviceInfo> sdfs = new Dictionary<string, DeviceInfo>();
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            string strOutput = string.Empty;
            p.StandardInput.WriteLine("busybox.exe fdisk -l");
            p.StandardInput.Write(p.StandardInput.NewLine);               //p.StandardInput.WriteLine("exit");
            //strOutput = p.StandardOutput.ReadToEnd();
            //MessageBox.Show(strOutput);
            if (!p.HasExited)
            {
                strOutput = p.StandardOutput.ReadToEnd();
                p.Close();
              string []sddddddd=  strOutput.Split(new string[] { "\n" }, StringSplitOptions.None);
              foreach (string s in sddddddd)
              {
                  MessageBox.Show(s);
              }
                //while ((strOutput = p.StandardOutput.ReadLine()) != null)
                //{
                //    if (strOutput.Contains("Disk /dev/sd"))
                //    {
                //        //MessageBox.Show(strOutput);                      
                //        if (strOutput.Split(':').Length == 2)
                //        {
                //            //sdfs.Add(strOutput.Split(':')[0], strOutput.Split(':')[1]);
                //            DeviceInfo di = new DeviceInfo();
                //            di.DeviceName = strOutput.Split(':')[0].ToLower().Replace("disk", " ").Trim();
                //            string shortsize = strOutput.Split(':')[1];
                //            if (shortsize.Split(',').Length == 2)
                //            {
                //                di.DeviceSize = shortsize.Split(',')[0];
                //                string longsize = shortsize.Split(',')[1].ToLower().Replace("bytes", " ").Trim();
                //                di.DeviceLength = Convert.ToInt64(longsize);
                //                if (di.DeviceLength <= 16041885696) {
                //                    sdfs.Add(di.DeviceName, di);
                                   
                //                    comboBoxSelectDrive.Invoke(new comboboxAddHandler(AddDriveItem), new object[] { strOutput });
                //                }
                //            }
                //            //MessageBox.Show(di.DeviceName + @"|" + di.DeviceLength);
                //        }
                //    }                   
                //}
/***********************************>>>>问题:strOutput 获得输出了,读完后cmd不退出
如果写这个busybox.exe dd bs=4096k if=atomDemo2g_20090515.img of=/dev/sdb 命令也没法判断是否执行,郁闷,不知道原因
*/
                st.Abort();
                st = null;
                p.StandardInput.WriteLine("exit");
                olvDevices.SetObjects(sdfs.Values);
            }
            p.WaitForExit();
            //
            p.Close();

解决方案 »

  1.   

    问题看得不是很明白。busybox.exe是什么样的程序?为何要通过cmd而不直接加载?
      

  2.   


       Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                string strOutput = string.Empty;
                p.StandardInput.WriteLine(cmdinfo);
    /*
             
    E:\EMP600\workspace\WrtImg\WrtImg\bin\Release>busybox.exe dd bs=4096k if=H:\boot
    \imgs\maxdos.img of=/dev/sdb
    2+1 records in
    2+1 records out
    */
    ///已经做到这里,读不到records out
                bool finished = false;
                while (!finished) {
                    strOutput = p.StandardOutput.ReadLine();//《--------被阻塞在这里了
                    if (strOutput.Contains("records out")) {
                        MessageBox.Show(strOutput);
                        p.StandardInput.WriteLine("exit");
                        finished = true;
                    }
                
                
                } 
                p.WaitForExit();
                //p.Kill();
                p.Close();
    程序目的
    在windows下,做一个2G的LINUX恢复盘
      

  3.   

    烦躁cmd执行可以显示
    E:\EMP600\workspace\WrtImg\WrtImg\bin\Release>busybox.exe dd bs=4096k if=H:\boot
    \imgs\maxdos.img of=/dev/sdb
    2+1 records in
    2+1 records out
    MessageBox.Show() 就是没有records out