disk.htm

来自「对于学习很有帮助」· HTM 代码 · 共 383 行

HTM
383
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

<HTML>
<HEAD>
	<TITLE>UDDF - DISK</TITLE>
	<META NAME="Description" CONTENT="Disk and Files section of the Delphi Developers FAQ" >
	<META NAME="Keywords" CONTENT="" >

</HEAD>
<BODY  bgcolor="#FFFFFF">
<CENTER>
<IMG SRC="../images/uddf.jpg"> </CENTER>
<HR SIZE="6" COLOR="LIME">

<CENTER><FONT SIZE="7" FACE="Arial Black" COLOR="RED">Disks and Files</FONT></CENTER>


<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<P><H1><A NAME="disk0">How to get files "Last Accessed" attribute?</P></A></H1>
<P><I>From: Jon Erik Oterhals &lt;jonoter@stud.ntnu.no&gt;</I></P>

<PRE>Brian Fl&oslash;e S&oslash;rensen wrote:

 In Windows 95, you can see when a file was last accessed by right-clicking
 the file and selecting properties.

 How can I get this information in Delphi/API???

</PRE>

<HR><PRE>procedure TForm1.Button1Click(Sender: TObject);
var
  FileHandle : THandle;
  LocalFileTime : TFileTime;
  DosFileTime : DWORD;
  LastAccessedTime : TDateTime;
  FindData : TWin32FindData;
begin
  FileHandle := FindFirstFile('AnyFile.FIL', FindData);
  if FileHandle &lt;> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      FileTimeToDosDateTime(LocalFileTime,
      LongRec(DosFileTime).Hi,LongRec(DosFileTime).Lo);
      LastAccessedTime := FileDateToDateTime(DosFileTime);
      Label1.Caption := DateTimeToStr(LastAccessedTime);
    end;
  end;
end;
</PRE><HR>

<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<H1><A NAME="disk1">How do I convert &quot;Long File Name.pas&quot; to &quot;longfi~1.pas&quot;?</A></H1>
<I><P>From: &quot;DynaSoft.&quot; &lt;TimH@onaustralia.com.au&gt;</P>
</I><P>Here try these procedures.</P>
<HR><PRE>Function GetShortFileName(Const FileName : String) : String;
var
  aTmp: array[0..255] of char;
begin
  if GetShortPathName(PChar(FileName),aTmp,Sizeof(aTmp)-1)=0 then
     Result:= FileName
  else
     Result:=StrPas(aTmp);
end;

Function GetLongFileName(Const FileName : String) : String;
var
  aInfo: TSHFileInfo;
begin
  if SHGetFileInfo(PChar(FileName),0,aInfo,Sizeof(aInfo),SHGFI_DISPLAYNAME)&lt;&gt;0 then
     Result:= String(aInfo.szDisplayName)
  else
     Result:= FileName;
end;</PRE><HR>

<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<P><H1><A NAME="disk2">HDD Serial Number</P></A></H1>
<I><P>From: Christian Piene Gundersen &lt;j.c.p.gundersen@jusstud.uio.no&gt;</P>
</I><PRE>&gt; We need to know how can we get the serial number of a HDD, working with
&gt; Delphi 2.0</PRE>
<P>Try this:</P>
<HR><PRE>procedure TForm1.Button1Click(Sender: TObject);
var
  SerialNum : pdword;
  a, b : dword;
  Buffer  : array [0..255] of char;
begin
  if GetVolumeInformation('c:\', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0) then
    Label1.Caption := IntToStr(SerialNum^);
end;</PRE><HR>

<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<H1><A NAME="disk3">How to check if drive 'a:' is ready?</A></H1>
<I><P>From: &quot;Angus Johnson&quot; &lt;ajohnson@rpi.net.au&gt;</P>
</I><HR><PRE>function DiskInDrive(const Drive: char): Boolean;
var
  DrvNum: byte;
  EMode: Word;
begin
  result := false;
  DrvNum := ord(Drive);
  if DrvNum &gt;= ord('a') then dec(DrvNum,$20);
  EMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    if DiskSize(DrvNum-$40) &lt;&gt; -1 then result := true
    else messagebeep(0);
  finally
    SetErrorMode(EMode);
  end;
end;</PRE><HR>


<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<H1><A NAME="disk4">Audio CD</A></H1>
<I><P>From: &quot;Chris Rankin&quot; &lt;RankinC@Logica.com&gt;</P>
</I><PRE>Vincent Oostindie &lt;vincent.oostindie@tip.nl&gt; wrote
&gt; * How do I get the unique number from an audio CD in the CD-ROM drive?
</PRE>
<HR><PRE>
const
  MCI_INFO_PRODUCT                = $00000100;
  MCI_INFO_FILE                   = $00000200;
  MCI_INFO_MEDIA_UPC              = $00000400;
  MCI_INFO_MEDIA_IDENTITY         = $00000800;
  MCI_INFO_NAME                   = $00001000;
  MCI_INFO_COPYRIGHT              = $00002000;

{ parameter block for MCI_INFO command message }
type
  PMCI_Info_ParmsA = ^TMCI_Info_ParmsA;
  PMCI_Info_ParmsW = ^TMCI_Info_ParmsW;
  PMCI_Info_Parms = PMCI_Info_ParmsA;
  TMCI_Info_ParmsA = record
    dwCallback: DWORD;
    lpstrReturn: PAnsiChar;
    dwRetSize: DWORD;
  end;
  TMCI_Info_ParmsW = record
    dwCallback: DWORD;
    lpstrReturn: PWideChar;
    dwRetSize: DWORD;
  end;
  TMCI_Info_Parms = TMCI_Info_ParmsA;</PRE><HR>

<P>These are the buffers you want: the identifier is returned as a string of decimal digits by the 
MCI_INFO_MEDIA_IDENTITY function. You should be able to cross-reference this with the online help (Win32 and TMediaPlayer component).</P>

<H1><A NAME="disk5">How can I delete a file to the Recycle Bin?</A></H1>
<I>From: "Ed Lagerburg" &lt;lagerbrg@euronet.nl&gt;</I>

<HR><PRE>
program del;

uses
 ShellApi;

//function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall;

Var T:TSHFileOpStruct;
    P:String;
begin
  P:='C:\Windows\System\EL_CONTROL.CPL';
  With T do
  Begin
    Wnd:=0;
    wFunc:=FO_DELETE;
    pFrom:=Pchar(P);
    fFlags:=FOF_ALLOWUNDO
  End;
  SHFileOperation(T);
End.
<HR></PRE>

<I>From: bstowers@pobox.com (Brad Stowers)</I><p>

There are some other quirks you should be aware of, too:
<UL><LI> Give the complete file path for every file specified.  Do not rely on the
current directory, even if you change to it right before the call.  The
SHFileOperation API is not "smart" enough to use the current directory if
one is not given for undo information.  So, even if you give the
FOF_ALLOWUNDO flag, it will not move deleted files to the recycle bin
because it doesn't know what path they came from, and thus couldn't restore
them to their original location from the recycle bin.  It will simply
delete the file from the current directory. <p>

<LI> MS has a documentation correction about the pFrom member.  It says that
for multiple files, each filename is seperated by a NULL (#0) character,
and the whole thing is terminated by double NULLs.  You need the double
NULL whether or not you are passing multiple filenames.  Sometimes it will
work correctly without them, but often not.  That's because sometimes you
get lucky and the memory following the end of your string has a NULL there. <p>
</UL>

An example of how to do this would be:<p>

<PRE>
var
  FileList: string;
  FOS: TShFileOpStruct;
begin
  FileList := 'c:\delete.me'#0'c:\windows\temp.$$$'#0#0;
  { if you were using filenames in string variables: }
  FileList := Filename1 + #0 + Filename2 + #0#0;

  FOS.pFrom := PChar(FileList);

  // blah blah blah
end;
</PRE>

<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<P><H1><A NAME="disk6">getting datetime problem<IMG SRC="../images/new.gif" WIDTH=28 HEIGHT=11 BORDER=0 ALT=" [NEW]"></P></A></H1>
<P><I>From: laserjet &lt;laserjet@concentric.net&gt;</I></P>


Try the following function which does not require FindFirst: <p>
 
<HR><PRE> function GetFileDate(TheFileName: string): string;
 var
   FHandle: integer;
 begin
   FHandle := FileOpen(TheFileName, 0);
   result := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle)));
   FileClose(FHandle);
 end;
</PRE><HR> 

<P><I>From: bziegler@Radix.Net (Ben Ziegler)</I></P>

One note of caution, some of the Win32 functions return times
in GMT time, and  you have to convert it to local time.  Check
your docs to be sure.  (FindNextFile does this I believe).


<p><H1><A NAME="disk7">Coping of the files<img src="../images/new.gif" width=28 height=11 border=0 alt=" [NEW]"></p></A></H1>

I have diffculties with coping the files. Delphi don't want to compile
LZCopy command.<p>

this way it work very slow<p>

<Hr><PRE>pbBuf := PChar( LocalAlloc(LMEM_FIXED, 1) );

FileSeek(source,0,0);
FileSeek(dest,0,0);
repeat
    cbRead := Fileread(source, pbBuf, 1);
    FileWrite(dest, pbBuf, cbRead);
until (cbRead = 0);</PRE><HR>

<H2>Solution 1</H2>
[Niel Calitz, omremcon@iafrica.com]

<Hr><PRE>{  You must add LZExpand to your uses clause  ea. USES LZExpand; }
function CopyFile(SrcF,DestF : string) : boolean;
var
  SFile,
  DFile : integer;
  Res   : longint;
  Msg   : string;

begin
  SFile := FileOpen(SrcF,0);        { Open ReadOnly = 0, Write=1, Readwrite=2}
  DFile := FileCreate(DestF);
  Res := LZCopy(SFile,DFile);
  FileClose(SFile);
  FileClose(DFile);
  if Res &lt; 0 then
  begin
    Msg := 'Unknown error';
    case Res of
      LZERROR_BADINHANDLE   : Msg := 'Invalid Source file handle';
      LZERROR_BADOUTHANDLE  : Msg := 'Invalid Destination file handle';
      LZERROR_BADVALUE      : Msg := 'Input parameter is out of range';
      LZERROR_GLOBALLOC     : Msg := 'Insufficient memory for the required buffers';
      LZERROR_GLOBLOCK      : Msg := 'Internal data structure handle invalid';
      LZERROR_READ          : Msg := 'Source file format is not valid';
      LZERROR_UNKNOWNALG    : Msg := 'The Source file was compressed with an unrecognized compression algorithm';
      LZERROR_WRITE         : Msg := 'There is insufficient space for the output file';
    end;
    MessageDlg(Msg,mtERROR,[mbOK],0);
    result := FALSE
  end else
    result := TRUE;
end;
</PRE><HR>
<H2>Solution 2</H2>
<I>[Tadas Vizbaras, tavizb@rc.lrs.lt]</I><p>

I'll bet it's slow! It's reading the file one character at a time... Try
allocating 8192 bytes and reading 8192 bytes at a time. That should speed
it up a bit...<p>

<H2>Solution 3</H2>

<I>[Sid Gudes, cougar@roadrunner.com]</I><P>

The simplest way to copy files is this:

<Hr><PRE>                VAR
                         sI,dI:Longint;
                        sD,sS:TFilename;

                USES LZExpand;
                        ............
                  sI := FileOpen(sS,fmShareDenyWrite);
                 dI := FileCreate(sD);
                  { Copy file }
                   CopyLZFile(sI,dI);
                  {close files}
                 FileClose(sI);
                 FileClose(dI);
                        ............
</PRE><HR>
<P><H1><A NAME="disk8">Shortened Directory label<IMG SRC="../images/new.gif" WIDTH=28 HEIGHT=11 BORDER=0 ALT=" [NEW]"></P></A></H1>

If the directory label is:
<PRE>c:\windows\media\temp\abc\sound\chime.wav</PRE>
I would like the label to appear as:
<PRE>c:\windows\..\sound\chime.wav</PRE>
and not the whole chunk of filename.<br>
Is there any way to accomplish this easily?

<I>[Stephan Meyer, sm006ns@munich.netsurf.de]</I><P>
I developed a procedure, that does something like that.
It shortens the path, when it and the current path have
the same drive and/or directory in parts.
It's really useful for making the pathname easier to read
and understand.
I've written it for a hex-editor in Borland Pascal and I haven't been
using it for a while, but it should work flawlessly.<p>

<HR><PRE>
function shortenfilename(s : string) : string;
var drive,curdrive : string[2];
    dir,curdir : string[80];
    name : string[20];
    ext : string[5];
    i : byte;
begin
  for i:=1 to length(s) do s[i]:=upcase(s[i]);
  s:=fexpand(s);
  fsplit(s,dir,name,ext);
  drive:=copy(dir,1,2);
  dir:=copy(dir,4,length(dir)-3);
  getdir(0,curdir);
  curdrive:=copy(curdir,1,2);
  curdir:=copy(curdir,4,length(curdir)-3)+'\';
  if drive=curdrive then begin
    if copy(dir,1,length(curdir))=curdir then begin
      i:=length(curdir);
      if length(dir)<>i then dir:=dir+'\';
      shortenfilename:=copy(dir,i+1,length(dir)-i-1)+name+ext;
    end else shortenfilename:=copy(s,3,length(s)-2);
  end else shortenfilename:=s;
end;</PRE><HR>

<HR SIZE="6" COLOR="LIME">
<FONT SIZE="2">
<a href="mailto:rdb@ktibv.nl">Please email me</a> and tell me if you liked this page.<BR>
<SCRIPT LANGUAGE="JavaScript">
<!--
	document.write("Last modified " + document.lastModified);
// -->
</SCRIPT><P>
<TABLE BORDER=0 ALIGN="CENTER">
<TR>
	<TD>This page has been created with </TD>
	<TD> <A HREF="http://www.dexnet.com./homesite.html"><IMG SRC="../images/hs25ani.gif" WIDTH=88 HEIGHT=31 BORDER=0 ALT="HomeSite 2.5b">
</A></TD>
</TR>
</TABLE>

</FONT>



</BODY>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?