C#實現后台格式化U盤的功能


檢測U盤:

1                 DriveInfo[] s = DriveInfo.GetDrives();
2                 var result = string.Empty;
3                 foreach (DriveInfo i in s)
4                 {
5                     if (i.DriveType == DriveType.Removable)  //這里就是可移動的設備了
6                     {
7                           break;
8                     }
9                 }

 

 

格式化磁盤:

        /// <summary>
        /// 格式化磁盤
        /// </summary>
        /// <param name="DiskName">盤符名稱:C:、D:、E:、F:</param>
        /// <returns></returns>
        public bool FormatDisk(string DiskName)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.UseShellExecute = false;

            Process process = Process.Start(processStartInfo);

            if (process != null)
            {
                process.StandardInput.WriteLine($"FORMAT {DiskName} /y /FS:FAT32 /V:BMECG /Q");
                process.StandardInput.Close();

                string outputString = process.StandardOutput.ReadToEnd();
                if (outputString.Contains("已完成"))
                {
                    return true;
                }
            }
            return false;
        }

 

使用批處理 : 

FORMAT G: /Y /FS:NTFS /V:My_LABEL /Q

其中:

 G: is a drive letter for formating.  (需要格式化的磁盤)

/Y is used to force the format and bypass the confirmation (確認)

Do you really want to format the drive ?
Yes /No

 /FS is used to choose the file system either FAT32 Or NTFS  (設置格式)

/V is for labeling the drive after format  (格式化后的磁盤名)

/Q is used to perform quick format   (使用快速格式化)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM