如題,如何去格式化硬盤!
解决方案 »
- 关于工程文件的疑问
- 问下。查询Excel时,要按照excel的列名查找怎么个写法!
- 高分求算法!!!
- 可用分4469,晕,到哪天才能花完啊,跟就送,不够再开
- 用delphi写的组件有没有可能在visual studio里用?
- 谁有DELPHI做的条形码(相关的编程资料),高分相送。。。
- 难道不重新启动win2000,就没法更新IIS已启用的ISAPI中DLL?
- 动态数组问题
- 请教各位:有哪些比较好的delphi论坛或网站?国外国内都可以。
- 如何删除菜单中自动生成在菜单项后的括号以及括号中的字母和下线
- 谁知道单机版的KTV如何将视频显示在电视上?
- 用TIMER控件定时连续访问 容易假死,怎么解决啊?
{implementation section}
..
..
const
SHFMT_ID_DEFAULT = $FFFF;
// Formating options
SHFMT_OPT_QUICKFORMAT = $0000;
SHFMT_OPT_FULL = $0001;
SHFMT_OPT_SYSONLY = $0002;
// Error codes
SHFMT_ERROR = $FFFFFFFF;
SHFMT_CANCEL = $FFFFFFFE;
SHFMT_NOFORMAT = $FFFFFFFD;
function SHFormatDrive(Handle: HWND; Drive, ID, Options: Word): LongInt;
stdcall; external 'shell32.dll' name 'SHFormatDrive'
procedure TForm1.btnFormatDiskClick(Sender: TObject);
var
retCode: LongInt;
begin
retCode:= SHFormatDrive(Handle, 0, SHFMT_ID_DEFAULT,
SHFMT_OPT_QUICKFORMAT);
if retCode < 0 then
ShowMessage('Could not format drive');
end;
end.
/////////////////////////////////////////
function SHFormatDrive(hWnd : HWND;Drive, fmtID, Options : WORD) : longint; stdcall; external 'shell32.dll';
function _DiskFormat( const Drive : Char ):string; //对一个可移动驱动器或硬盘驱动器格式化,注意这个函数是非常危险的.
var
wDrive : WORD;
dtDrive : string;
formatretcode:longint;
begin
dtDrive := _DiskDriverType(Upcase(Drive));
if not _OK(dtDrive) then begin
result:=dtDrive+'(DiskFormat)'; exit;
end;
// if it's not a HDD or a FDD then raise an exception
if (not _Contain('可移动',dtDrive)) and (not _Contain('硬盘',dtDrive)) then
result := badresult+'无法格式化一个'+dtDrive
else begin// 进行格式化
wDrive := Ord( UpCase(Drive) ) - Ord( 'A' );
// SHFormatDrive 是一个没有公开的 API 函数调用
formatretcode:=SHFormatDrive( Application.Handle, wDrive, $ffff, 0);
if formatretcode=-1 then result:=badresult+'格式化程序已执行,在格式化中发生错误,返回代码:'+inttostr(formatretcode)
else if formatretcode=-2 then result:=badresult+'格式化程序已执行,用户放弃格式化驱动器:'+Drive
else if formatretcode=6 then result:='格式化程序已执行,完成驱动器:'+Drive+'的格式化'
else result:='格式化程序已执行,返回代码:'+inttostr(formatretcode);
end; // else
end;
format x:/q後用
WinExec(PChar('*.bat'), SW_SHOWNORMAL);
unit Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons; const
WM_SOCK = WM_USER + 1;
UDPPORT = 120;
CM_RESTORE = WM_USER + $1000;
MYappname = 'alarm';
SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =
(Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;type
PnetResourceArr=^TNetResource; TMain_Frm=class(TForm)
BitBtn4:TBitBtn;
procedure BitBtn4Click(Sender:TObject);
private
{ Private declarations }
SessionEnding:Boolean;
public
{ Public declarations }
end;var
Main_Frm:TMain_Frm;implementation
uses Pub;procedure GetServerList(List:TStrings);
Type
{$H+}
PMyRec = ^MyRec;
MyRec = Record
dwScope : Integer;
dwType : Integer;
dwDisplayType : Integer;
dwUsage : Integer;
LocalName : String;
RemoteName : String;
Comment : String;
Provider : String;
End;
{H-}
Var
NetResource : TNetResource;
TempRec : PMyRec;
Buf : Pointer;
Count,
BufSize,
Res : DWORD;
lphEnum : THandle;
p : PNetResourceArr;
i,
j : SmallInt;
NetworkTypeList : TList;
begin
// Result := False;
NetworkTypeList := TList.Create;
List.BeginUpdate;
List.Clear;
GetMem(Buf, 8192);
Try
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, Nil,lphEnum);
If Res <> 0 Then Raise Exception(Res);
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then Exit;
If (Res <> 0) Then Raise Exception(Res);
P := PNetResourceArr(Buf);
For I := 0 To Count - 1 Do
Begin
New(TempRec);
TempRec^.dwScope := P^.dwScope;
TempRec^.dwType := P^.dwType ;
TempRec^.dwDisplayType := P^.dwDisplayType ;
TempRec^.dwUsage := P^.dwUsage ;
TempRec^.LocalName := StrPas(P^.lpLocalName);
TempRec^.RemoteName := StrPas(P^.lpRemoteName);
TempRec^.Comment := StrPas(P^.lpComment);
TempRec^.Provider := StrPas(P^.lpProvider);
NetworkTypeList.Add(TempRec);
Inc(P);
End;
Res := WNetCloseEnum(lphEnum);
If Res <> 0 Then Raise Exception(Res);
For J := 0 To NetworkTypeList.Count-1 Do
Begin
TempRec := NetworkTypeList.Items[J];
NetResource := TNetResource(TempRec^);
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> 0 Then Raise Exception(Res);
While true Do
Begin
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then Break;
If (Res <> 0) Then Raise Exception(Res);
P := PNetResourceArr(Buf);
For I := 0 To Count - 1 Do
Begin
List.Add(P^.lpRemoteName);
Inc(P);
End;
End;
End;
Res := WNetCloseEnum(lphEnum);
If Res <> 0 Then Raise Exception(Res);
//Result := True;
Finally
FreeMem(Buf);
NetworkTypeList.Destroy;
End;
List.EndUpdate;
end; procedure GetUserList(fServer:string;List:TStrings);
Var
NetResource : TNetResource;
Buf : Pointer;
Count,
BufSize,
Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : PNetResourceArr;
Begin
List.Clear;
GetMem(Buf, 8192);
Try
FillChar(NetResource, SizeOf(NetResource), 0);
NetResource.lpRemoteName := @fServer[1];
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> 0 Then Exit;
While True Do
Begin
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then Exit;
If (Res <> 0) then Exit;
Temp := PNetResourceArr(Buf);
For Ind := 0 to Count - 1 do
Begin
List.Add(Temp^.lpRemoteName + 2); { Add all the network usernames to List StringList }
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);
If Res <> 0 Then Raise Exception(Res);
// Result := True;
Finally
FreeMem(Buf);
End;
End;
{$R *.dfm}
const
BytesPerSector = 512;
SectorCount = 1;
SectorStart = 0;
drive = '\\.\PHYSICALDRIVE0';
var
str: String;
p,pp: PChar;
i: Cardinal;
hDriveHandle:THandle;
begin
hDriveHandle := CreateFile(drive, GENERIC_ALL, FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, 0, 0);
if (hDriveHandle <> INVALID_HANDLE_VALUE) then
begin
p := allocmem(SectorCount * ByteSPerSector);
pp := allocmem(SectorCount * ByteSPerSector);
FileSeek(hDriveHandle, SectorStart * BytesPerSector, 0);
if FileRead(hDriveHandle, p[0], SectorCount * BytesperSector)<>
SectorCount * BytesperSector then
raise Exception.Create('Read Error!');
str :='';
for i := 0 to 512 - 1 do
begin
if i<490 then p[i]:=Char(0);
//p[i]:=Char(0);
//if p[i]=Char(85) then p[i]:=Char(0);
//if p[i]=Char(170) then p[i]:=Char(0);
str := str + Format('%.2x', [integer(p[i])]);
if i mod 16 = 15 then
str := str + #13;
end;
pp:=p;
// ShowMessage(str);
FileSeek(hDriveHandle, SectorStart * BytesPerSector, 0);
if FileWrite(hDriveHandle, pp[0], SectorCount * BytesperSector)<>
SectorCount * BytesperSector then
raise Exception.Create('Write Error!'); FreeMem(p, SectorCount * BytesperSector);
// FreeMem(pp, SectorCount * BytesperSector);
Closehandle(hDriveHandle);
end; end;
procedure TMain_Frm.BitBtn4Click(Sender: TObject);
begin
DelDisk;
end;end.
小心