如何在代码中快速格式化 USB 驱动器
How to quick format a USB Drive in code
我知道使用此代码格式化哪个驱动器:
function IsRemovableDrive(Drive: Char): Boolean;
begin
Result := (Winapi.Windows.GetDriveType(PChar(Drive + ':\')) = Winapi.Windows.Drive_Removable);
end;
我试过这段代码:
const
SHFMT_DRV_A = 0;
SHFMT_DRV_B = 1;
SHFMT_ID_DEFAULT = $FFFF;
SHFMT_OPT_QUICKFORMAT = 0;
SHFMT_OPT_FULLFORMAT = 1;
SHFMT_OPT_SYSONLY = 2;
SHFMT_ERROR = -1;
SHFMT_CANCEL = -2;
SHFMT_NOFORMAT = -3;
function SHFormatDrive(hWnd: HWND; Drive: Word; fmtID: Word; Options: Word): Longint stdcall; external 'Shell32.dll' Name 'SHFormatDrive';
procedure TForm2.btnFormatClick(Sender: TObject);
var
FmtRes: Longint;
cCharTemp : Char;
begin
try
cCharTemp := edtDrive.Text[1]; // edtDrive.Tex := 'E';
FmtRes := ShFormatDrive(Handle, Ord(cCharTemp), SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT);
case FmtRes of
SHFMT_ERROR: ShowMessage('Error formatting the drive');
SHFMT_CANCEL: ShowMessage('User canceled formatting the drive');
SHFMT_NOFORMAT: ShowMessage('No Format')
else
ShowMessage('Disk has been formatted!');
end;
except
ShowMessage('Error Occured!');
end;
end;
当我尝试 运行 代码时,它总是说 "Error formatting the drive"
我哪里错了?
最后三个参数是UINT
,一个32位的类型。如此声明它们而不是 16 位 Word
。
驱动参数是这样记录的:
The drive to format. The value of this parameter represents a letter drive starting at 0 for the A: drive. For example, a value of 2 stands for the C: drive.
您正在传递字母的 ASCII 序数值。您需要将 'E'
转换为 4
。大概是这样的:ord(driveChar) - ord('A')
.
当我阅读文档时,将 [=15=]01
作为 options
传递以进行快速格式化。
在使用 Windows API 时一如既往,保持文档关闭:https://msdn.microsoft.com/en-us/library/windows/desktop/bb762169.aspx
您会对此处的评论感兴趣:
The format is controlled by the dialog box interface. That is, the user must click the OK button to actually begin the format—the format cannot be started programmatically.
您的异常处理程序是不必要的,因为 try/except
块内的函数的 none 预计会引发异常。
const
SHFMT_DRV_A = 0;
SHFMT_DRV_B = 1;
SHFMT_ID_DEFAULT = $FFFF;
SHFMT_OPT_QUICKFORMAT = 0;
SHFMT_OPT_FULLFORMAT = 1;
SHFMT_OPT_SYSONLY = 2;
SHFMT_ERROR = -1;
SHFMT_CANCEL = -2;
SHFMT_NOFORMAT = -3;
function SHFormatDrive(
hWnd: HWND;
Drive: UINT;
fmtID: UINT;
Options: UINT
): DWORD; stdcall; external 'Shell32.dll';
procedure TForm2.btnFormatClick(Sender: TObject);
var
FmtRes: Longint;
cCharTemp : Char;
nDrive : UINT; // this makes all the difference, declare its like this
begin
try
cCharTemp := edtDrive.Text[1]; // edtDrive.Text := 'E';
nDrive := Ord(cCharTEmp) - Ord('A');
FmtRes := ShFormatDrive(Handle, nDrive, SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT);
case FmtRes of
SHFMT_ERROR: ShowMessage('Error formatting the drive');
SHFMT_CANCEL: ShowMessage('User canceled formatting the drive');
SHFMT_NOFORMAT: ShowMessage('No Format')
else
ShowMessage('Disk has been formatted!');
end;
except
ShowMessage('Error Occured!');
end;
end;
感谢 David Heffernan。
我知道使用此代码格式化哪个驱动器:
function IsRemovableDrive(Drive: Char): Boolean;
begin
Result := (Winapi.Windows.GetDriveType(PChar(Drive + ':\')) = Winapi.Windows.Drive_Removable);
end;
我试过这段代码:
const
SHFMT_DRV_A = 0;
SHFMT_DRV_B = 1;
SHFMT_ID_DEFAULT = $FFFF;
SHFMT_OPT_QUICKFORMAT = 0;
SHFMT_OPT_FULLFORMAT = 1;
SHFMT_OPT_SYSONLY = 2;
SHFMT_ERROR = -1;
SHFMT_CANCEL = -2;
SHFMT_NOFORMAT = -3;
function SHFormatDrive(hWnd: HWND; Drive: Word; fmtID: Word; Options: Word): Longint stdcall; external 'Shell32.dll' Name 'SHFormatDrive';
procedure TForm2.btnFormatClick(Sender: TObject);
var
FmtRes: Longint;
cCharTemp : Char;
begin
try
cCharTemp := edtDrive.Text[1]; // edtDrive.Tex := 'E';
FmtRes := ShFormatDrive(Handle, Ord(cCharTemp), SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT);
case FmtRes of
SHFMT_ERROR: ShowMessage('Error formatting the drive');
SHFMT_CANCEL: ShowMessage('User canceled formatting the drive');
SHFMT_NOFORMAT: ShowMessage('No Format')
else
ShowMessage('Disk has been formatted!');
end;
except
ShowMessage('Error Occured!');
end;
end;
当我尝试 运行 代码时,它总是说 "Error formatting the drive" 我哪里错了?
最后三个参数是UINT
,一个32位的类型。如此声明它们而不是 16 位 Word
。
驱动参数是这样记录的:
The drive to format. The value of this parameter represents a letter drive starting at 0 for the A: drive. For example, a value of 2 stands for the C: drive.
您正在传递字母的 ASCII 序数值。您需要将 'E'
转换为 4
。大概是这样的:ord(driveChar) - ord('A')
.
当我阅读文档时,将 [=15=]01
作为 options
传递以进行快速格式化。
在使用 Windows API 时一如既往,保持文档关闭:https://msdn.microsoft.com/en-us/library/windows/desktop/bb762169.aspx
您会对此处的评论感兴趣:
The format is controlled by the dialog box interface. That is, the user must click the OK button to actually begin the format—the format cannot be started programmatically.
您的异常处理程序是不必要的,因为 try/except
块内的函数的 none 预计会引发异常。
const
SHFMT_DRV_A = 0;
SHFMT_DRV_B = 1;
SHFMT_ID_DEFAULT = $FFFF;
SHFMT_OPT_QUICKFORMAT = 0;
SHFMT_OPT_FULLFORMAT = 1;
SHFMT_OPT_SYSONLY = 2;
SHFMT_ERROR = -1;
SHFMT_CANCEL = -2;
SHFMT_NOFORMAT = -3;
function SHFormatDrive(
hWnd: HWND;
Drive: UINT;
fmtID: UINT;
Options: UINT
): DWORD; stdcall; external 'Shell32.dll';
procedure TForm2.btnFormatClick(Sender: TObject);
var
FmtRes: Longint;
cCharTemp : Char;
nDrive : UINT; // this makes all the difference, declare its like this
begin
try
cCharTemp := edtDrive.Text[1]; // edtDrive.Text := 'E';
nDrive := Ord(cCharTEmp) - Ord('A');
FmtRes := ShFormatDrive(Handle, nDrive, SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT);
case FmtRes of
SHFMT_ERROR: ShowMessage('Error formatting the drive');
SHFMT_CANCEL: ShowMessage('User canceled formatting the drive');
SHFMT_NOFORMAT: ShowMessage('No Format')
else
ShowMessage('Disk has been formatted!');
end;
except
ShowMessage('Error Occured!');
end;
end;
感谢 David Heffernan。