using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text; namespace Metasharp
{
public class DriveManager
{
#region SetLabel /// <summary>
/// set a drive label to the desired value
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <returns>true if success, false if failure</returns>
public static bool SetLabel(char driveLetter, string label = "")
{
#region args check if (!Char.IsLetter(driveLetter))
{
return false;
}
if (label == null)
{
label = "";
} #endregion
try
{
DriveInfo di = DriveInfo.GetDrives()
.Where(d => d.Name.StartsWith(driveLetter.ToString()))
.FirstOrDefault();
di.VolumeLabel = label;
return true;
}
catch (Exception)
{
return false;
}
} #endregion #region FormatDrive /// <summary>
/// Format a drive using the best available method
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <param name="quickFormat">quick formatting?</param>
/// <param name="enableCompression">enable drive compression?</param>
/// <param name="clusterSize">cluster size (default=null for auto). Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
/// <returns>true if success, false if failure</returns>
public static bool FormatDrive(char driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
{
return FormatDrive_CommandLine(driveLetter, label, fileSystem, quickFormat, enableCompression, clusterSize);
} #endregion #region FormatDrive_CommandLine /// <summary>
/// Format a drive using Format.com windows file
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <param name="quickFormat">quick formatting?</param>
/// <param name="enableCompression">enable drive compression?</param>
/// <param name="clusterSize">cluster size (default=null for auto). Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
/// <returns>true if success, false if failure</returns>
public static bool FormatDrive_CommandLine(char driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
{
#region args check if (!Char.IsLetter(driveLetter) ||
!IsFileSystemValid(fileSystem))
{
return false;
} #endregion
bool success = false;
string drive = driveLetter + ":";
try
{
var di = new DriveInfo(drive);
var psi = new ProcessStartInfo();
psi.FileName = "format.com";
psi.WorkingDirectory = Environment.SystemDirectory;
psi.Arguments = "/FS:" + fileSystem +
" /Y" +
" /V:" + label +
(quickFormat ? " /Q" : "") +
((fileSystem == "NTFS" && enableCompression) ? " /C" : "") +
(clusterSize.HasValue ? " /A:" + clusterSize.Value : "") +
" " + drive;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
var formatProcess = Process.Start(psi);
var swStandardInput = formatProcess.StandardInput;
swStandardInput.WriteLine();
formatProcess.WaitForExit();
success = true;
}
catch (Exception) { }
return success;
} #endregion #region FormatDrive_Shell32 #region interop // http://msdn.microsoft.com/en-us/library/windows/desktop/bb762169(v=vs.85).aspx
[DllImport("shell32.dll")]
private static extern uint SHFormatDrive(IntPtr hwnd, uint drive, SHFormatFlags fmtID, SHFormatOptions options); private enum SHFormatFlags : uint
{
SHFMT_ID_DEFAULT = 0xFFFF,
/// <summary>
/// A general error occured while formatting. This is not an indication that the drive cannot be formatted though.
/// </summary>
SHFMT_ERROR = 0xFFFFFFFF,
/// <summary>
/// The drive format was cancelled by user/OS.
/// </summary>
SHFMT_CANCEL = 0xFFFFFFFE,
/// <summary>
/// A serious error occured while formatting. The drive is unable to be formatted by the OS.
/// </summary>
SHFMT_NOFORMAT = 0xFFFFFFD
} [Flags]
private enum SHFormatOptions : uint
{
/// <summary>
/// Full formatting
/// </summary>
SHFMT_OPT_COMPLETE = 0x0,
/// <summary>
/// Quick Format
/// </summary>
SHFMT_OPT_FULL = 0x1,
/// <summary>
/// MS-DOS System Boot Disk
/// </summary>
SHFMT_OPT_SYSONLY = 0x2
} #endregion /// <summary>
/// Format a drive using Shell32.dll
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <param name="quickFormat">quick formatting?</param>
/// <returns>true if success, false if failure</returns>
[Obsolete("Unsupported by Microsoft nowadays. Prefer the FormatDrive() or FormatDrive_CommandLine() methods")]
public static bool FormatDrive_Shell32(char driveLetter, string label = "", bool quickFormat = true)
{
#region args check if (!Char.IsLetter(driveLetter))
{
return false;
} #endregion
bool success = false;
string drive = driveLetter + ":";
try
{
var di = new DriveInfo(drive);
var bytes = Encoding.ASCII.GetBytes(di.Name.ToCharArray());
uint driveNumber = Convert.ToUInt32(bytes[] - Encoding.ASCII.GetBytes(new[] { 'A' })[]);
var options = SHFormatOptions.SHFMT_OPT_COMPLETE;
if (quickFormat)
options = SHFormatOptions.SHFMT_OPT_FULL; uint returnCode = SHFormatDrive(IntPtr.Zero, driveNumber, SHFormatFlags.SHFMT_ID_DEFAULT, options);
if (returnCode == (uint)SHFormatFlags.SHFMT_ERROR)
throw new Exception("An error occurred during the format. This does not indicate that the drive is unformattable.");
else if (returnCode == (uint)SHFormatFlags.SHFMT_CANCEL)
throw new OperationCanceledException("The format was canceled.");
else if (returnCode == (uint)SHFormatFlags.SHFMT_NOFORMAT)
throw new IOException("The drive cannot be formatted."); SetLabel(driveLetter, label);
success = true;
}
catch (Exception) { }
return success;
} #endregion #region FormatDrive_Win32Api // http://msdn.microsoft.com/en-us/library/aa394515(VS.85).aspx /// <summary>
/// Format a drive using Win32 API
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <param name="quickFormat">quick formatting?</param>
/// <param name="enableCompression">enable drive compression?</param>
/// <param name="clusterSize">cluster size. Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
/// <returns>true if success, false if failure</returns>
[Obsolete("Might have troubles formatting ram drives. Prefer the FormatDrive() or FormatDrive_CommandLine() methods")]
public static bool FormatDrive_Win32Api(char driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int clusterSize = )
{
#region args check if (!Char.IsLetter(driveLetter) ||
!IsFileSystemValid(fileSystem))
{
return false;
} #endregion
bool success = false;
try
{
var moSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_Volume WHERE DriveLetter='" + driveLetter + ":'");
foreach (ManagementObject mo in moSearcher.Get())
{
mo.InvokeMethod("Format", new object[] { fileSystem, quickFormat, clusterSize, label, enableCompression });
success = true;
}
}
catch (Exception)
{
success = false;
}
return success;
} #endregion #region IsFileSystemValid /// <summary>
/// test if the provided filesystem value is valid
/// </summary>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <returns>true if valid, false if invalid</returns>
public static bool IsFileSystemValid(string fileSystem)
{
#region args check if (fileSystem == null)
{
return false;
} #endregion
switch (fileSystem)
{
case "FAT":
case "FAT32":
case "EXFAT":
case "NTFS":
case "UDF":
return true;
default:
return false;
}
} #endregion
}
}

Link: http://www.metasharp.net/index.php/Format_a_Hard_Drive_in_Csharp

http://buffernow.com/format-drive-using-c/

最新文章

  1. RecyclerView的使用之多种Item加载布局
  2. 在其他系统Iframe中显示SharePoint 页面
  3. sphinx 配置文件全解析
  4. HDU 5884 (贪心)
  5. SQL Server创建复合索引时,复合索引列顺序对查询的性能影响
  6. 你所不知道的Android Studio调试技巧
  7. IOS UISearchDisplayController 点击搜索出现黑条问题解决方案
  8. 防止sql注入,过滤敏感关键字
  9. 【JavaScript】Object.observe()带来的数据绑定变革
  10. 学习笔记-[Maven实战]-第三章:Maven使用入门(1)
  11. ASP.NET- LinkButton 传递多个参数
  12. iPhone6设计自适应布局
  13. Xamarin.Android 在VS下调试时提示 In mgmain JNI_OnLoad 程序“Mono”已退出 解决办法
  14. freeMark的入门教程
  15. win10下 anaconda 环境下python2和python3版本转换
  16. gunicorn启动flask项目的坑
  17. python 闯关之路四(上)(并发编程与数据库理论)
  18. iOS 定时器 NSTimer、CADisplayLink、GCD3种方式的实现
  19. ORA错误总结
  20. ubuntu crontab python 定时任务备记

热门文章

  1. android下调用C,JNI调用
  2. [转载]findContours函数参数说明及相关函数
  3. maven寻找jar
  4. 图解javascript中this指向
  5. git代码提交方式
  6. Perl 语法 - 高级特性
  7. java final
  8. Shader中贴图知识汇总: 漫反射贴图、凹凸贴图、高光贴图、 AO贴图、环境贴图、 光照纹理及细节贴图
  9. Windows下Git的安装及配置
  10. LDM和STM指令