我想试一下用Delphi做一个程序,运行的结果是要格式化软盘或者硬盘的,有没有这类的API函数呢?具体的怎么应用,我用100分得具体的原代码,谢谢各位

解决方案 »

  1.   

    可以用shellapi里面去执行windows下面的format.exe 用/y参数不需要确认,但是不可以格式化当前系统分区
      

  2.   

    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; 
      

  3.   

    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; 
      

  4.   

    这样得到一个Windows中的格式化窗体啊,我是要直接的格式,什么都不要提示的那种
      

  5.   

    实在太简单了
    winexec('format c:',sw_hide);
      

  6.   

    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;