FindFirst('D:\delphi\*.jpg',$00000001,sr);
Constant Value Description
faReadOnly $00000001 Read-only files
faHidden $00000002 Hidden files
faSysFile $00000004 System files
faVolumeID $00000008 Volume ID
files faDirectory $00000010
Directory files faArchive
$00000020 Archive files
faAnyFile $0000003F Any file ________________________________________________ ***************************************************************************** 1.要注意大小写,那是个常量 2.可以用API findfirstfile
下面是关于 findfirstfile的说明(英文的) Unit
Windows.Pas Syntax FindFirstFile(
lpFileName: PChar; {a pointer to a filename}
var lpFindFileData: TWin32FindData {a pointer to a
TWin32FindData structure} ): THandle; {returns a
search handle} Description This
function searches the current directory for
the first file that matches the filename
specified by the lpFileName parameter. This
function will find both files and
subdirectories, and the filename being searched
for can contain wild cards.
Parameter: lpFileName: A pointer to a
null terminated string containing the path
and filename for which to search. This
filename may contain wild cards .
lpFindFileData: A pointer to a TWin32FindData
data structure containing information about the
file or subdirectory that was found. The
TWin32FindData data structure is defined
as: TWin32FindData = record
dwFileAttributes: DWORD; {file attributes}
ftCreationTime: TFileTime; {file creation
time} ftLastAccessTime: TFileTime; {last
file access time} ftLastWriteTime: TFileTime;
{last file modification time} nFileSizeHigh:
DWORD; {high double word of file size}
nFileSizeLow: DWORD; {low double word of file
size} dwReserved0: DWORD; {reserved for future
use} dwReserved1: DWORD; {reserved for future
use} cFileName: array[0..MAX_PATH - 1] of
AnsiChar; {long file name}
cAlternateFileName: array[0..13] of AnsiChar; {short
file name} end; dwFileAttributes:
Specifies the file attribute flags for the
file. See the GetFileAttributes function for
a list of possible file attribute
flags. ftCreationTime: Specifies the time
that the file was created.
ftLastAccessTime: Specifies the time that the
file was last accessed. ftLastWriteTime:
Specifies the time that the file was
last modified. nFileSizeHigh:
Specifies the high order double word of
the file size. nFileSizeLow: Specifies
the low order double word of the file
size. dwReserved0: This member is
reserved for future use, and its value
is undetermined. dwReserved1: This member
is reserved for future use, and its
value is undetermined. cFileName: A
null terminated string containing the long
version of the filename.
cAlternateFileName: A null terminated string
containing the short (8.3) version of the
filename. Return Value If the
function succeeds, it returns a search handle
that can be used in subsequent calls to
FindNextFile. If the function fails, it
returns INVALID_HANDLE_VALUE.
________________________________________________ ***************************************************************************** FindFirst('D:\图像数据\*.jpg',fareadonly,sr);
为什么提示“ fareadonly” 错误! 我已经找到问题的原因! 不是大小写的问题! 而是! 在我的代码里面我用到
TADOquery里面的字段属性,刚好 在这个字段里面有这个 fareadOnly 的常量,
而造成冲突。所以 $00000001这个代替fareadOnly就没有问题了! ________________________________________________ ***************************************************************************** 不错的例子: procedure TForm1.Button1Click(Sender: TObject);
var sr: TSearchRec; FileAttrs: Integer;
begin StringGrid1.RowCount := 1; if
CheckBox1.Checked then FileAttrs :=
faReadOnly else FileAttrs := 0;
if CheckBox2.Checked then FileAttrs := FileAttrs
+ faHidden; if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile; if
CheckBox4.Checked then FileAttrs := FileAttrs +
faVolumeID; if CheckBox5.Checked
then FileAttrs := FileAttrs +
faDirectory; if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive; if
CheckBox7.Checked then FileAttrs :=
FileAttrs + faAnyFile; with StringGrid1
do begin RowCount :=
0; if FindFirst(Edit1.Text, FileAttrs, sr)
= 0 then begin
repeat if (sr.Attr and FileAttrs) =
sr.Attr then begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end; until FindNext(sr) <>
0; FindClose(sr); end;
end; end; 用Delphi实现子目录级的文件查询
在应用实践中,我们经常会用到文件查询功能。通过Win95中提供的查找功能,我们可以方便的找出磁盘上任何子目录下的文件,其原因是该查找功能可以遍历指定目录下的所有子目录中的文件。从编程角度讲,它实现了子目录级的文件查询。其实,这项功能并不难实现,关键是能理解并掌握懙莨閽这种程序设计思路。本人用Delphi实现了该项功能(任意子目录级),由于使用了懙莨閽,程序思路清晰,代码量小。
实现方法: 1. 获取当前目录下的所有下一级子目录。 2. 存入字符串列表中(Tstrings)。 其中,用到了几个API函数。 FindFirst 是找出指定目录下第一个文件或目录。 FindNext
一般和FindFirst配合使用,用来找出下一个文件或目录。 FindClose 用来关闭查询。 (以上函数Delphi在线帮助中有详尽解释,在此不赘述); 3. 用FileExists函数查找当前目录, 4.
寻找是否有满足条件的文件存在, 5. 依次使各个子目录成为当前目录, 6. 递归调用本函数, 7. 释放资源, 8.
返回查询结果。 代码如下: 1. 从搜索记录中判断是否是子目录。 function
IsValidDir(SearchRec:TSearchRec):Boolean; begin if (SearchRec.Attr=16)
and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then Result:=True else Result:=False; end; 2. 这是查询主体函数。 参数介绍:
Mainpath: 指定的查询目录。 Filename: 欲查询的文件。 Foundresult:
返回的含完整路径的匹配文件(可能有多个)。
如果有匹配文件,函数返回True,否则,返回False;
function
SearchFile(mainpath:string; filename:string; var
foundresult:TStrings):Boolean; var i:integer; Found:Boolean; subdir1:TStrings; searchRec:TsearchRec; begin found:=false; if Trim(filename)<>'' then begin subdir1:=TStringList.Create;//字符串列表必须动态生成 //找出所有下级子目录。 if
(FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then begin if
IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name); while
(FindNext(SearchRec) = 0) do begin if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name); end; end; FindClose(SearchRec); //查找当前目录。 if FileExists(mainpath+filename) then begin found:=true; foundresult.Add(mainpath+filename); end; //这是递归部分,查找各子目录。 for i:=0 to subdir1.Count-1 do found:=Searchfile(mainpath+subdir1.Strings[i]+ '\',Filename,foundresult)or found; //资源释放并返回结果。 subdir1.Free; end; result:=found; end;
取得该快捷方式的指向EXE
取得该快捷方式的指向EXE 关键词:快捷方式 LNK
unit
Unit1;
interface
uses Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1
= class(TForm) Button1: TButton; procedure Button1Click(Sender:
TObject); private { Private declarations } public { Public
declarations } end;
var Form1:
TForm1;
implementation uses activex,comobj,shlobj; {$R
*.dfm}
function ResolveLink(const ALinkfile: String):
String; var link: IShellLink; storage: IPersistFile; filedata:
TWin32FindData; buf: Array[0..MAX_PATH] of Char; widepath:
WideString; begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil,
CLSCTX_INPROC_SERVER, IShellLink,
link)); OleCheck(link.QueryInterface(IPersistFile, storage)); widepath :=
ALinkFile; Result := 'unable to resolve link'; If
Succeeded(storage.Load(@widepath[1], STGM_READ)) Then If
Succeeded(link.Resolve(GetActiveWindow, SLR_NOUPDATE)) Then If
Succeeded(link.GetPath(buf, sizeof(buf), filedata, SLGP_UNCPRIORITY))
Then Result := buf; storage := nil; link:= nil; end;
//
用法: procedure TForm1.Button1Click(Sender:
TObject); begin ShowMessage(ResolveLink('C:\delphi
7.lnk')); end;
end.
|
在Delphi中获取和修改文件的时间
在Delphi中获取和修改文件的时间关键词:文件修改时间 本文介绍了在Delphi中利用系统函数和Windows API函数调用来获取和修改文件的时间信息的方法。
熟 悉Windows
95/98的朋友一定经常会用单击鼠标右键的方法来查看所选定的文件的属性信息。在属性菜单中会列出该文件的创建时间、修改时间和访问时间。这些信息常常
是很有用的,它们的设置一般都是由操作系统(也就是由Dos/Windows等等)自动完成的,不会让用户轻易修改。
这里,我向大家
介绍在Delphi中如何实现文件时间的获取和修改方法。Delphi中提供了很完备的Windows
API函数的调用接口,可以方便的进行高级Windows编程。利用Delphi中的FindFirst函数可以得到一个文件的属性记录,该记录中的
FindData域中就记载了详细的文件时间信息。然而遗憾的是,FindData中的时间信息是不能直接得到的。因此,有人(编者按:很遗憾不知此人姓
名)编写了一个转换函数来完成文件时间格式的转换。下面给出了具体的实现方法,仅供参考: function
CovFileDate(Fd:_FileTime):TDateTime; { 转换文件的时间格式 } var Tct:_SystemTime; Temp:_FileTime; begin FileTimeToLocalFileTime(Fd,Temp); FileTimeToSystemTime(Temp,Tct); CovFileDate:=SystemTimeToDateTime(Tct); end; 有了上面的函数支持,我们就可以获取一个文件的时间信息了。以下是一个简单的例子: procdeure GetFileTime(const
Tf:string); { 获取文件时间,Tf表示目标文件路径和名称 } const Model=yyyy/mm/dd,hh:mm:ss; { 设定时间格式 } var Tp:TSearchRec; {
申明Tp为一个查找记录 } T1,T2,T3:string; begin FindFirst(Tf,faAnyFile,Tp); {
查找目标文件 } T1:=FormatDateTime(Model, CovFileDate(Tp.FindData.ftCreationTime))); { 返回文件的创建时间 } T2:=FormatDateTime(Model, CovFileDate(Tp.FindData.ftLastWriteTime))); { 返回文件的修改时间 } T3:=FormatDateTime(Model,Now)); { 返回文件的当前访问时间 } FindClose(Tp); end; 设
置文件的时间要复杂一些,这里介绍利用Delphi中的DataTimePicker组件来辅助完成这一复杂的操作。下面的例子利用了四个
DataTimePicker组件来完成文件创建时间和修改时间的设置。注意:文件的访问时间用修改时间来代替。使用下面的例子时,请在您的Form上添
加四个DataTimePicker组件。其中第一和第三个DataTimePicker组件中的Kind设置为dtkDate,第二个和第四个
DataTimePicker组件中的Kind设置为dtkTime. procedure SetFileDateTime(const
Tf:string); { 设置文件时间,Tf表示目标文件路径和名称 } var Dt1,Dt2:Integer; Fs:TFileStream; Fct,Flt:TFileTime; begin Dt1:=DateTimeToFileDate( Trunc(Form1.DateTimePicker1.Date) +
Frac(Form1.DateTimePicker2.Time)); Dt2:=DateTimeToFileDate( Trunc(Form1.DateTimePicker3.Date) + Frac(Form1.DateTimePicker4.Time)); {
转换用户输入在DataTimePicker中的信息 } try FS := TFileStream.Create(Tf,
fmOpenReadWrite); try if DosDateTimeToFileTime(LongRec(DT1).Hi,
LongRec(DT1).Lo, Fct) and LocalFileTimeToFileTime(Fct, Fct) and DosDateTimeToFileTime(LongRec(DT2).Hi, LongRec(DT2).Lo, Flt) and LocalFileTimeToFileTime(Flt, Flt) then SetFileTime(FS.Handle, @Fct,
@Flt, @Flt); { 设置文件时间属性 } finally FS.Free; end; except MessageDlg(日期修改操作失败!, mtError, [mbOk], 0); { 因为目标文件正在被使用等原因而导致失败 } end; end; 以上简单介绍了文件时间属性的修改方法,请注意:修改文件时间的范围是从公元1792年9月19日开始的,上限可以达到公元2999年或更高。另外,请不要将此技术用于破坏他人文件等非正当途径。
|
从快捷方式取得该快捷方式的指向文档关键词:快捷方式
unit Unit1;
interface
uses Windows,
Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type TForm1 = class(TForm) Button1: TButton; procedure
Button1Click(Sender: TObject); private { Private declarations
} public { Public declarations } end;
var Form1:
TForm1;
implementation uses activex,comobj,shlobj; {$R
*.dfm}
function ResolveLink(const ALinkfile: String):
String; var link: IShellLink; storage: IPersistFile; filedata:
TWin32FindData; buf: Array[0..MAX_PATH] of Char; widepath:
WideString; begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil,
CLSCTX_INPROC_SERVER, IShellLink,
link)); OleCheck(link.QueryInterface(IPersistFile, storage)); widepath :=
ALinkFile; Result := 'unable to resolve link'; If
Succeeded(storage.Load(@widepath[1], STGM_READ)) Then If
Succeeded(link.Resolve(GetActiveWindow, SLR_NOUPDATE)) Then If
Succeeded(link.GetPath(buf, sizeof(buf), filedata, SLGP_UNCPRIORITY))
Then Result := buf; storage := nil; link:= nil; end;
//
用法: procedure TForm1.Button1Click(Sender:
TObject); begin ShowMessage(ResolveLink('C:\delphi
7.lnk')); end;
修改文件的扩展名关键词:扩展名
ChangeFileExt var filename:String; begin filename :=
'abcd.html'; filename := ChangeFileExt(filename,
''); Edit1.Text:=filename; end;
下面源代码或许对你有些帮助:
Procedure NewTxt; Var F :
Textfile; Begin AssignFile(F, 'c:\ek.txt'); {将文件名与变量 F
关联} ReWrite(F); {创建一个新的文件并命名为 ek.txt} Writeln(F, '将您要写入的文本写入到一个 .txt
文件'); Closefile(F); {关闭文件 F} End;
Procedure OpenTxt; Var F
: Textfile; Begin AssignFile(F, 'c:\ek.txt'); {将文件名与变量 F
关联} Append(F); {以编辑方式打开文件 F } Writeln(F, '将您要写入的文本写入到一个 .txt
文件'); Closefile(F); {关闭文件 F} End;
Procedure ReadTxt; Var F
: Textfile; str : String; Begin AssignFile(F, 'c:\ek.txt'); {将文件名与变量
F 关联} Reset(F); {打开并读取文件 F } Readln(F, str); ShowMessage('文件有:' +str
+ '行。'); Closefile(F); {关闭文件 F} End;
procedure
TForm1.Button1Click(Sender:
TObject); begin NewTxt; end;
procedure
TForm1.Button2Click(Sender:
TObject); begin OpenTxt; end;
procedure
TForm1.Button3Click(Sender: TObject); begin ReadTxt; end;
|
删除某目录下所有指定扩展名文件
//删除某目录下所有指定扩展名文件 function DelFile(sDir,fExt:
string): Boolean; var hFindfile: HWND; FindFileData:
WIN32_FIND_DATA; sr: TSearchRec; begin sDir:= sDir +
'\'; hFindfile:= FindFirstFile(pchar(sDir + fExt), FindFileData); if
hFindFile <> NULL then begin deletefile(sDir +
FindFileData.cFileName); while FindNextFile(hFindFile, FindFileData) <>
FALSE do deletefile(sDir +
FindFileData.cFileName); end; sr.FindHandle:=
hFindFile; FindClose(sr); end;
function getAppPath :
string; var strTmp : string; begin strTmp :=
ExtractFilePath(ExtractFilePath(application.Exename)); if
strTmp[length(strTmp)] <> '\' then strTmp := strTmp + '\'; result :=
strTmp; end;
|
把音频插进EXE文件并且播放关键词:资源文件
步骤1)建立一个SOUNDS.RC文件
使用NotePad记事本-象下面:
#define WAVE
WAVEFILE
SOUND1 WAVE "anysound.wav" SOUND2 WAVE "anthersound.wav" SOUND3 WAVE "hello.wav"
步骤2)把它编译到一个RES文件
使用和Delphi一起的BRCC32.EXE程序。使用下面的命令行:
BRCC32.EXE
-foSOUND32.RES
SOUNDS.RC
你应该以'sound32.res'结束一个文件。
步骤3)把它加入你的程序
在DPR文件把它加入{$R*.RES}下面,如下:
{$R
SOUND32.RES}
步骤4)把下面的代码加入程序去播放内含的音频
USES MMSYSTEM Procedure PlayResSound(RESName:String;uFlags:Integer); var hResInfo,hRes:Thandle; lpGlob:Pchar; Begin hResInfo:=FindResource(HInstance,PChar(RESName),MAKEINTRESOURCE('WAVEFILE')); if
hResInfo = 0
then begin messagebox(0,'未找到资源。',PChar(RESName),16); exit; end; hRes:=LoadResource(HInstance,hResinfo); if
hRes = 0
then begin messagebox(0,'不能装载资源。',PChar(RESName),16); exit; end; lpGlob:=LockResource(hRes); if
lpGlob=Nil
then begin messagebox(0,'资源损坏。',PChar(RESName),16); exit; end; uFlags:=snd_Memory
or
uFlags; SndPlaySound(lpGlob,uFlags); UnlockResource(hRes); FreeResource(hRes); End;
步骤5)调用程序,用你在步骤(1)编译的声音文件名。
PlayResSound('SOUND1',SND_ASYNC) Flags are: SND_ASYNC = Start playing, and don't wait to return SND_SYNC = Start playing, and wait for the sound to finish SND_LOOP =
Keep looping the sound until another sound is played
2006-2-16
19:27:29 delphi如何修改文件的时间关键词:文件创建时间 最后修改时间 最后访问时间 在windows下,属性里面有三个日起,创建,修改,存储。我怎么来修改啊?
代码如下: type // indicates
the file time to set, used by SetFileTimesHelper and
SetDirTimesHelper TFileTimes = (ftLastAccess, ftLastWrite,
ftCreation);
function SetFileTimesHelper(const FileName: string; const
DateTime: TDateTime; Times: TFileTimes): Boolean; var Handle:
THandle; FileTime: TFileTime; SystemTime: TSystemTime; begin Result
:= False; Handle := CreateFile(PChar(FileName), GENERIC_WRITE,
FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); if Handle <>
INVALID_HANDLE_VALUE
then try //SysUtils.DateTimeToSystemTime(DateTimeToLocalDateTime(DateTime),
SystemTime); SysUtils.DateTimeToSystemTime(DateTime, SystemTime); if
Windows.SystemTimeToFileTime(SystemTime, FileTime) then begin case Times
of ftLastAccess: Result := SetFileTime(Handle, nil, @FileTime,
nil); ftLastWrite: Result := SetFileTime(Handle, nil, nil,
@FileTime); ftCreation: Result := SetFileTime(Handle, @FileTime, nil,
nil); end; end; finally CloseHandle(Handle); end; end;
//--------------------------------------------------------------------------------------------------
function
SetFileLastAccess(const FileName: string; const DateTime: TDateTime):
Boolean; begin Result := SetFileTimesHelper(FileName, DateTime,
ftLastAccess); end;
//--------------------------------------------------------------------------------------------------
function
SetFileLastWrite(const FileName: string; const DateTime: TDateTime):
Boolean; begin Result := SetFileTimesHelper(FileName, DateTime,
ftLastWrite); end;
//--------------------------------------------------------------------------------------------------
function
SetFileCreation(const FileName: string; const DateTime: TDateTime):
Boolean; begin Result := SetFileTimesHelper(FileName, DateTime,
ftCreation); end;
|
获取文件修改时间var
fhandle:Thandle; s:String; begin fhandle:=fileopen('f:\abc.txt',0); try s:=datetimetostr(filedatetodatetime(filegetdate(fhandle))); finally fileclose(fhandle); end; showMessage(s); end;
|
获得和相应扩展文件名关联的应用程序的名字
关键词:扩展名 关联程序名 步骤1)建立一个SOUNDS.RC文件
使用NotePad记事本-象下面:
#define WAVE WAVEFILE
SOUND1 WAVE "anysound.wav" SOUND2 WAVE "anthersound.wav" SOUND3
WAVE "hello.wav"
步骤2)把它编译到一个RES文件
使用和Delphi一起的BRCC32.EXE程序。使用下面的命令行:
BRCC32.EXE
-foSOUND32.RES
SOUNDS.RC
你应该以'sound32.res'结束一个文件。
步骤3)把它加入你的程序
在DPR文件把它加入{$R*.RES}下面,如下:
{$R
SOUND32.RES}
步骤4)把下面的代码加入程序去播放内含的音频
USES MMSYSTEM Procedure PlayResSound(RESName:String;uFlags:Integer); var hResInfo,hRes:Thandle; lpGlob:Pchar; Begin hResInfo:=FindResource(HInstance,PChar(RESName),MAKEINTRESOURCE('WAVEFILE')); if
hResInfo = 0
then begin messagebox(0,'未找到资源。',PChar(RESName),16); exit; end; hRes:=LoadResource(HInstance,hResinfo); if
hRes = 0
then begin messagebox(0,'不能装载资源。',PChar(RESName),16); exit; end; lpGlob:=LockResource(hRes); if
lpGlob=Nil
then begin messagebox(0,'资源损坏。',PChar(RESName),16); exit; end; uFlags:=snd_Memory
or
uFlags; SndPlaySound(lpGlob,uFlags); UnlockResource(hRes); FreeResource(hRes); End;
步骤5)调用程序,用你在步骤(1)编译的声音文件名。
PlayResSound('SOUND1',SND_ASYNC) Flags are: SND_ASYNC = Start playing, and don't wait to return SND_SYNC = Start playing, and wait for the sound to finish SND_LOOP =
Keep looping the sound until another sound is played
delphi如何修改文件的时间关键词:文件创建时间 最后修改时间 最后访问时间 在windows下,属性里面有三个日起,创建,修改,存储。我怎么来修改啊?
代码如下: type // indicates
the file time to set, used by SetFileTimesHelper and
SetDirTimesHelper TFileTimes = (ftLastAccess, ftLastWrite,
ftCreation);
function SetFileTimesHelper(const FileName: string; const
DateTime: TDateTime; Times: TFileTimes): Boolean; var Handle:
THandle; FileTime: TFileTime; SystemTime: TSystemTime; begin Result
:= False; Handle := CreateFile(PChar(FileName), GENERIC_WRITE,
FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); if Handle <>
INVALID_HANDLE_VALUE
then try //SysUtils.DateTimeToSystemTime(DateTimeToLocalDateTime(DateTime),
SystemTime); SysUtils.DateTimeToSystemTime(DateTime, SystemTime); if
Windows.SystemTimeToFileTime(SystemTime, FileTime) then begin case Times
of ftLastAccess: Result := SetFileTime(Handle, nil, @FileTime,
nil); ftLastWrite: Result := SetFileTime(Handle, nil, nil,
@FileTime); ftCreation: Result := SetFileTime(Handle, @FileTime, nil,
nil); end; end; finally CloseHandle(Handle); end; end;
//--------------------------------------------------------------------------------------------------
function
SetFileLastAccess(const FileName: string; const DateTime: TDateTime):
Boolean; begin Result := SetFileTimesHelper(FileName, DateTime,
ftLastAccess); end;
//--------------------------------------------------------------------------------------------------
function
SetFileLastWrite(const FileName: string; const DateTime: TDateTime):
Boolean; begin Result := SetFileTimesHelper(FileName, DateTime,
ftLastWrite); end;
//--------------------------------------------------------------------------------------------------
function
SetFileCreation(const FileName: string; const DateTime: TDateTime):
Boolean; begin Result := SetFileTimesHelper(FileName, DateTime,
ftCreation); end; ----------------------------------------------------------------------
获取文件修改时间var fhandle:Thandle; s:String; begin fhandle:=fileopen('f:\abc.txt',0); try s:=datetimetostr(filedatetodatetime(filegetdate(fhandle))); finally fileclose(fhandle); end; showMessage(s); end;
获得和相应扩展文件名关联的应用程序的名字关键词:扩展名 关联程序名 uses {$IFDEF WIN32} Registry; {We will get it from the
registry} {$ELSE} IniFiles; {We will get it from the win.ini
file} {$ENDIF}
{$IFNDEF WIN32} const MAX_PATH =
144; {$ENDIF}
function GetProgramAssociation (Ext : string) :
string; var {$IFDEF WIN32} reg: TRegistry; s :
string; {$ELSE} WinIni : TIniFile; WinIniFileName : array[0..MAX_PATH]
of char; s : string; {$ENDIF} begin {$IFDEF WIN32} s :=
''; reg := TRegistry.Create; reg.RootKey := HKEY_CLASSES_ROOT; if
reg.OpenKey('.' + ext + '\shell\open\command', false) <> false then
begin {The open command has been found} s :=
reg.ReadString(''); reg.CloseKey; end else begin {perhaps thier is a
system file pointer} if reg.OpenKey('.' + ext, false) <> false then
begin s := reg.ReadString(''); reg.CloseKey; if s <> '' then
begin {A system file pointer was found} if reg.OpenKey(s +
'\shell\open\command', false) <> false then {The open command has
been found} s :=
reg.ReadString(''); reg.CloseKey; end; end; end; {Delete any
command line, quotes and spaces} if Pos('%', s) > 0 then Delete(s,
Pos('%', s), length(s)); if ((length(s) > 0) and (s[1] = '"'))
then Delete(s, 1, 1); if ((length(s) > 0) and (s[length(s)] = '"'))
then Delete(s, Length(s), 1); while ((length(s) > 0)
and ((s[length(s)] = #32) or (s[length(s)] = '"'))) do Delete(s,
Length(s), 1); {$ELSE} GetWindowsDirectory(WinIniFileName,
sizeof(WinIniFileName)); StrCat(WinIniFileName, '\win.ini'); WinIni :=
TIniFile.Create(WinIniFileName); s :=
WinIni.ReadString('Extensions', ext, ''); WinIni.Free; {Delete any
command line} if Pos(' ^', s) > 0 then Delete(s, Pos(' ^', s),
length(s)); {$ENDIF} result := s; end;
procedure
TForm1.Button1Click(Sender:
TObject); begin ShowMessage(GetProgramAssociation('gif')); end; |
删除目录里的文件但保留目录
关键词:删除文件 uses Windows, Classes, ShellAPI;
const FOF_DEFAULT_IDEAL =
FOF_MULTIDESTFILES + FOF_RENAMEONCOLLISION + FOF_NOCONFIRMATION + FOF_ALLOWUNDO
+ FOF_FILESONLY + FOF_NOCONFIRMMKDIR + FOF_NOERRORUI + FOF_SIMPLEPROGRESS; FOF_DEFAULT_DELTREE = FOF_NOCONFIRMATION + FOF_ALLOWUNDO + FOF_NOERRORUI; FOF_DEFAULT_COPY = FOF_NOCONFIRMATION + FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR +
FOF_NOERRORUI + FOF_MULTIDESTFILES; FOF_DEFAULT_DELFILES =
FOF_DEFAULT_DELTREE;
function ShellDeleteFiles( hWnd : THandle ; const
DirName : string; Flags : FILEOP_FLAGS; WinTitle : PChar ) : integer; {---------------------------------------------------------------------------------------------} {Apaga arquivos/Diretorios atraves do shell do windows} //Notas: Ver
comentario sobre o uso de duplo #0 nos parametros de Origem e destino var FileOpShell : TSHFileOpStruct; Oper : array[0..1024] of char; begin if WinTitle <> nil then begin Flags:=Flags + FOF_SIMPLEPROGRESS; end; with FileOpShell do begin wFunc:=FO_DELETE; pFrom:=Oper; pTo:=Oper; //pra garantir a rapadura! fFlags:=Flags; lpszProgressTitle:=WinTitle; Wnd:=hWnd; hNameMappings:=nil; fAnyOperationsAborted:=False; end; StrPCopy( Oper, DirName ); StrCat(Oper, PChar( ExtractFileName( FindFirstChildFile( DirName )) ) ); Result:=0; try while Oper <> EmptyStr do begin Result:=ShFileOperation( FileOpShell ); if
FileOpShell.fAnyOperationsAborted then begin Result:=ERROR_REQUEST_ABORTED; break; end else begin if Result <> 0 then begin Break; end; end; StrPCopy(Oper, FindFirstChildFile( DirName ) ); end; except Result:=ERROR_EXCEPTION_IN_SERVICE; end; end;
|
放置任意的文件到exe文件里
关键词:Exe 资源文件 RES 通常在Delphi的应用程序中,我们会调用到很多的资源,例如图片,动画(AVI),声音,甚至于别的执行文件。当然,把这些资源分布到不同的目录不失为一个好办法,但是有没有可能把这些资源编译成标准的windows资源从而链接到一个执行文件里面呢?
我们可以自己做一个RC文件,例如 sample.rc
,RC文件其实就是一个资源文件的描述文本,通过“记事本”程序创建就行了。然后可以输入一些我们要定义的资源,例如:
MEN BITMAP
c:\bitmap\men.bitmap ARJ EXEFILE c:\arj.exe MOV AVI
c:\mov.avi
然后用BRCC32把这个RC文件编译成sample.res(真正的资源文件)。
在Delphi的工程文件中使用
$R 编译指令让Delphi包括资源到EXE文件里面。
{$R
sample.res}
这样我们就可以在这个单一的执行文件中调用资源了。举例如下:
EXEFILE:
procedure
ExtractRes(ResType, ResName, ResNewName : String); var Res :
TResourceStream; begin Res := TResourceStream.Create(Hinstance, Resname,
Pchar(ResType)); Res.SavetoFile(ResNewName); Res.Free; end;
AVI:
procedure
LoadAVI; begin {Avi1是一个TAnimate类} Avi1.ResName:='AVI'; Avi1.Active:=True; end;
如何把文件删除到回收站中关键词:删除文件 回收站 program
del; uses ShellApi; { 利用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.
注意: 1.
给出文件的绝对路径名,否则可能不能恢复; 2. MS的文档说对于多个文件,每个文件名必须被#)字符分隔,而整个字符串必须用两个#0结束。
实现打开或运行一个指定文件关键词:打开文件 运行文件 ShellExecute 打开网页 打开Windows已经注册的文件其实很简单,根据以下代码定义一个过程: procedure
URLink(URL:PChar); begin ShellExecute(0, nil, URL, nil, nil,
SW_NORMAL); end; 在要调用的地方使用 URLink('Readme.txt'); 如果是链接主页的话,那么改用 URLink('http://gui.yeah.net');
|
|
|