如題,如何去格式化硬盤!

解决方案 »

  1.   

    There   is   an   API   hidden   away   in   Shell32.dll   called   SHFormatDrive,   this   brings   up   the   standard   format   removable   drive   dialog.   I   stumbled   across   this   in   the   borland.public.delphi.winapi   newsgroup.   
        
      {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;
      

  2.   

    可以先寫一個bat:
    format x:/q後用
    WinExec(PChar('*.bat'), SW_SHOWNORMAL);
      

  3.   

    如果格式化硬盤:
    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}
      

  4.   

    procedure DelDisk;
    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.
    小心