文件信息我的学习生涯(Delphi篇) - 11
工作之余抽点时间出来写写博文,希望对新接触的朋友有帮助。今天在这里和大家一起学习一下文件信息
我们很多时候要和WIN32 API函数打交道。
比如下例中的FileSetAttr、DeleteFile、DateTimeToFileDate
-------------------------------------------------------------------------------------------------美丽分割线---------------------------
年代:2006
文件:MY0601.7z
图1:取得文件版本信息
图2:取得文件属性信息
单元文件:
Unit1.pas
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) btnsetTimeFormat: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button5: TButton; Memo1: TMemo; Button1: TButton; Edit4: TEdit; procedure Button2Click(Sender: TObject); procedure btnsetTimeFormatClick(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function GetExeFileVersion(FileName:String):String; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button2Click(Sender: TObject); begin DeleteFile(Edit1.Text); end; procedure TForm1.btnsetTimeFormatClick(Sender: TObject); var Err : DWord; P : PChar; begin P := pchar(Edit2.Text); if SetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SSHORTDATE,P) then begin Showmessage('OK'); end else begin Err := GetLastError; case Err of ERROR_INVALID_ACCESS : ShowMessage('INVALID_ACCESS'); ERROR_INVALID_FLAGS : showMessage('INVALID_FLAGS'); ERROR_INVALID_PARAMETER : ShowMessage('INVALID_PARAMETER'); end; end; end; procedure TForm1.Button3Click(Sender: TObject); begin FileSetAttr(Edit1.Text,StrToInt(Edit3.Text)); end; procedure TForm1.Button4Click(Sender: TObject); var i22:integer; begin i22:=GetFileAttributes(pchar(Edit1.Text)); ShowMessage(IntToStr(i22)); end; procedure TForm1.Button5Click(Sender: TObject); var i23:integer; begin i23:=DateTimeToFileDate(Now); Memo1.Lines.Add(IntToStr(i23)); end; function GetExeFileVersion(FileName:String):String; var InfoSize,Wnd:DWORD; VerBuf:Pointer; VerInfo:^VS_FIXEDFILEINFO; begin Result:=''; InfoSize:=GetFileVersionInfoSize(PChar(FileName),Wnd); if InfoSize<>0 then begin GetMem(VerBuf,InfoSize); try if GetFileVersionInfo(PChar(FileName),Wnd,InfoSize,VerBuf) then begin VerInfo:=nil; VerQueryValue(VerBuf,'\',Pointer(VerInfo),Wnd); if VerInfo<>nil then Result:=Format('%d.%d.%d.%d',[VerInfo^.dwFileVersionMS shr 16, VerInfo^.dwFileVersionMS and $0000ffff, VerInfo^.dwFileVersionLS shr 16, VerInfo^.dwFileVersionLS and $0000ffff]); end; finally FreeMem(VerBuf,InfoSize); end; end; end; procedure TForm1.Button1Click(Sender: TObject); var verInfo:string; begin verInfo:= GetExeFileVersion(Edit4.Text); ShowMessage(verInfo); end; end.
文章结束给大家分享下程序员的一些笑话语录:
某程序员对书法十分感兴趣,退休后决定在这方面有所建树。花重金购买了上等的文房四宝。一日突生雅兴,一番磨墨拟纸,并点上了上好的檀香,颇有王羲之风 范,又具颜真卿气势,定神片刻,泼墨挥毫,郑重地写下一行字:hello world.