2017年5月25日 星期四

[Automation] How to clean disk using diskpart command

How to clean disk using diskpart command
井民全, Jing, mqjing@gmail.com
Google doc: This Document

Usage

Command Line Version

diskpart /s cleanDisk.txt

cmd Version

cmd /c start /D . diskpart /s c:\Users\jing\CleanDisk.txt
e.g.

C# Version

Usage

      void Run_CleanDisk_Demo()
       {
           Run_CleanDisk();
       }

       void Run_CleanDisk()
       {
           String strProgramPath = ".";
           String strCommand = "diskpart /s c:\\Users\\jing\\CleanDisk.txt";

           Process proc = null;
           try
           {
               proc = InvokeCmd("cmd", "/c start /D " + strProgramPath + " " + strCommand);
           }
           catch (Exception e)
           {
               throw new Exception(string.Format("Error: Launch Program Failure. ", e.ToString()));
           }
       }


Function

       public static System.Diagnostics.Process InvokeCmd(string command, string commondArg = "", bool bWaitUIReady = false, string WorkingPath = "")
       {
           System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
           psi.FileName = command;
           psi.Arguments = commondArg;
           psi.WorkingDirectory = WorkingPath;

           if (true == bWaitUIReady)
           {
               psi.WindowStyle = ProcessWindowStyle.Normal;
           }
           else
           {
               psi.WindowStyle = ProcessWindowStyle.Hidden;
           }

           System.Diagnostics.Process ProcessInstance = System.Diagnostics.Process.Start(psi);
           if (null != ProcessInstance)
           {
               if (true == bWaitUIReady)
               {
                   try
                   {
                       ProcessInstance.WaitForInputIdle();
                   }
                   catch (InvalidOperationException)
                   {
                       Debug.Assert(false, "WaitForInputIdle Error!");
                   }
               }
              
           }
           return ProcessInstance;
       }



Diskpart Script

cleanDisk.txt
list disk
select disk 1
clean