Delphi中获取文件夹路径的三种方式 和实现文件复制
2014-07-11 10:47
786 查看
问题一:在Win98中右击“我的文档”,选属性,在弹出的“我的文档 属性”窗口
中点击“浏览”按钮就会弹出一个“浏览文件夹”对话框。请问这个对话框是怎么做出
来的?
答案:要做这个对话框有三种方法。
(一)第一种方法是用Delphi提供的组件(在Win 3.1面板上)模仿在上面看到的对
话框自己组装一个“浏览文件夹”窗体。具体的做法是:
1. 在你的Project里增加一个BorderStyle为bsDialog的新窗体;
2.放置一个DirectoryListBox组件;
3. 放置一个DriveComboBox组件,设置DirList为DirectoryListBox1;
4.然后再放上两个Button。一个“确定”(ModalResult为mrOk),一个“取消”
(ModalResult为mrCancel);
5.最后只要在调用这个浏览文件夹的地方加上一下代码就算大功告成了:
if Form2.ShowModal = mrOk then
Memo1.Lines.Add(Form2.DirectoryListBox1.Directory);
(二)第二种方法,在Delphi中可以通过调用SelectDirectory函数得到这种效果。
SelectDirectory在Delphi 4中的申明如下(请注意,一共有两个重载的申明):
type
TSelectDirOpt = (sdAllowCreate, sdPerformCreate, sdPrompt);
TSelectDirOpts = set of TSelectDirOpt;
function SelectDirectory(var Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload;
function SelectDirectory(const Caption: string; const Root:WideString; o
ut Directory: string): Boolean; overload;
第一种语法的Options参数指定了“浏览文件夹”对话框的一些选项;参数HelpCtx
指定上下文敏感的Help ID;Directory初始化了对话框的值,并且携带返回值。
第二种语法的Caption参数指定对话框标题(比如′请选择XXX的文件夹′);参数
Root指定用来浏览的根目录;所选择文件夹返回在参数Directory中。
不管是哪种语法,如果在对话框中选择了路径并按下“确定”按钮,SelectDirect
ory函数返回True;在其它情况下,函数SelectDirectory就返回False。
示例:
uses FileCtrl;(引用)
var
dir:string;
begin
SelectDirectory(‘选择’,‘’,dir);
end;
(三)第三种方法是比较高明的解决方案。在Windows中已经有一个专门用来处理这
种问题的ShellAPI函数——SHBrowseForFolder(事实上,第二种方法的第二种语法就是
调用了这个API,这在Delphi的源代码中可以得到证实)。因为它是使用系统已有的API
,这样就不会占用太多的系统资源,从而减小代码长度、提高程序运行速度,并且在Wi
ndows的不同语言版本中会自动的和Windows相适应。具体代码如下: (注:需要在uses中引入两个单元ShlObj,Windows)
var
Info: TBrowseInfo;
Dir: array[0..260] of char;
ItemId: PItemIDList;
begin
with Info do
begin
hwndOwner := self.Handle;
pidlRoot := nil;
pszDisplayName := nil;
lpszTitle := ′请选择XXX的文件夹′;
ulFlags := 0;
lpfn := nil;
lParam := 0;
iImage := 0;
end;
ItemId := SHBrowseForFolder(Info);
if ItemId <> nil then
begin
SHGetPathFromIDList(ItemId, @Dir);
Result := string(Dir);
end
else
Result := ′′;
end;
如果你对最后的这种方法感兴趣,以Browsing for Folders为主题在Windows API
Help中检索将会得到更多的文档。
问题二:在许多软件的制作过程中我都遇到文件复制这个问题,但我对文件操作很
不熟悉。请问,在Delphi中有简单的方法能够实现这个功能吗?
答案:实现它的源代码如下:
var
DesFile, SourFile: File;
Buf: Byte;
begin
AssignFile(SrcFile, ″c:\autoexec.bat″);
Reset(SrcFile, 1);// 1 = 逐个字节操作
AssignFile(DesFile, ″c:\autoexec.bak″);
Rewrite(DesFile, 1); // 同上
while not Eof(SrcFile) do
begin
BlockRead(SrcFile, Buf, SizeOf(Byte)); // 从源文件中读出来
BlockWrite(DesFile, Buf, SizeOf(Byte)); // 写到目标文件中去
end;
CloseFile(SrcFile);
CloseFile(DesFile);
end;
另外还有一个高招——直接使用API函数CopyFile。这个API的原型如下:
BOOL CopyFile(
LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
如:CopyFile( PChar(′c:\autoexec.bat′), PChar(′c:\autoexec.bak′), True);
Delphi如何实现浏览文件夹,并得出所选择的文件夹名:
SelectDirectory函数(FileCtrl单元)
1、function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean; overload;
2、function SelectDirectory(var Directory: string; Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload;
注意:使用前请uses FileCtrl;
第1种调用格式示例为:
const
sCaption = '文件夹'; //弹出框标题名(非弹出框窗体名)
sRoot = ''; //初始文件夹(如'C:\','D:\DownLoad'等, 不存在则从桌面)
var
sDir: string;
begin
if SelectDirectory(sCaption, sRoot, sDir) then
//已返回所选文件夹路径给sDir,自行处理
end;
第2种调用格式示例为:
const
SELDIRHELP = 1000;
var
sDir: string; //初始文件夹(如'C:\','D:\DownLoad'等)
begin
sDir := '';
if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt], SELDIRHELP) then
//已返回所选文件夹路径给sDir,自行处理
end;
中点击“浏览”按钮就会弹出一个“浏览文件夹”对话框。请问这个对话框是怎么做出
来的?
答案:要做这个对话框有三种方法。
(一)第一种方法是用Delphi提供的组件(在Win 3.1面板上)模仿在上面看到的对
话框自己组装一个“浏览文件夹”窗体。具体的做法是:
1. 在你的Project里增加一个BorderStyle为bsDialog的新窗体;
2.放置一个DirectoryListBox组件;
3. 放置一个DriveComboBox组件,设置DirList为DirectoryListBox1;
4.然后再放上两个Button。一个“确定”(ModalResult为mrOk),一个“取消”
(ModalResult为mrCancel);
5.最后只要在调用这个浏览文件夹的地方加上一下代码就算大功告成了:
if Form2.ShowModal = mrOk then
Memo1.Lines.Add(Form2.DirectoryListBox1.Directory);
(二)第二种方法,在Delphi中可以通过调用SelectDirectory函数得到这种效果。
SelectDirectory在Delphi 4中的申明如下(请注意,一共有两个重载的申明):
type
TSelectDirOpt = (sdAllowCreate, sdPerformCreate, sdPrompt);
TSelectDirOpts = set of TSelectDirOpt;
function SelectDirectory(var Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload;
function SelectDirectory(const Caption: string; const Root:WideString; o
ut Directory: string): Boolean; overload;
第一种语法的Options参数指定了“浏览文件夹”对话框的一些选项;参数HelpCtx
指定上下文敏感的Help ID;Directory初始化了对话框的值,并且携带返回值。
第二种语法的Caption参数指定对话框标题(比如′请选择XXX的文件夹′);参数
Root指定用来浏览的根目录;所选择文件夹返回在参数Directory中。
不管是哪种语法,如果在对话框中选择了路径并按下“确定”按钮,SelectDirect
ory函数返回True;在其它情况下,函数SelectDirectory就返回False。
示例:
uses FileCtrl;(引用)
var
dir:string;
begin
SelectDirectory(‘选择’,‘’,dir);
end;
(三)第三种方法是比较高明的解决方案。在Windows中已经有一个专门用来处理这
种问题的ShellAPI函数——SHBrowseForFolder(事实上,第二种方法的第二种语法就是
调用了这个API,这在Delphi的源代码中可以得到证实)。因为它是使用系统已有的API
,这样就不会占用太多的系统资源,从而减小代码长度、提高程序运行速度,并且在Wi
ndows的不同语言版本中会自动的和Windows相适应。具体代码如下: (注:需要在uses中引入两个单元ShlObj,Windows)
var
Info: TBrowseInfo;
Dir: array[0..260] of char;
ItemId: PItemIDList;
begin
with Info do
begin
hwndOwner := self.Handle;
pidlRoot := nil;
pszDisplayName := nil;
lpszTitle := ′请选择XXX的文件夹′;
ulFlags := 0;
lpfn := nil;
lParam := 0;
iImage := 0;
end;
ItemId := SHBrowseForFolder(Info);
if ItemId <> nil then
begin
SHGetPathFromIDList(ItemId, @Dir);
Result := string(Dir);
end
else
Result := ′′;
end;
如果你对最后的这种方法感兴趣,以Browsing for Folders为主题在Windows API
Help中检索将会得到更多的文档。
问题二:在许多软件的制作过程中我都遇到文件复制这个问题,但我对文件操作很
不熟悉。请问,在Delphi中有简单的方法能够实现这个功能吗?
答案:实现它的源代码如下:
var
DesFile, SourFile: File;
Buf: Byte;
begin
AssignFile(SrcFile, ″c:\autoexec.bat″);
Reset(SrcFile, 1);// 1 = 逐个字节操作
AssignFile(DesFile, ″c:\autoexec.bak″);
Rewrite(DesFile, 1); // 同上
while not Eof(SrcFile) do
begin
BlockRead(SrcFile, Buf, SizeOf(Byte)); // 从源文件中读出来
BlockWrite(DesFile, Buf, SizeOf(Byte)); // 写到目标文件中去
end;
CloseFile(SrcFile);
CloseFile(DesFile);
end;
另外还有一个高招——直接使用API函数CopyFile。这个API的原型如下:
BOOL CopyFile(
LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
如:CopyFile( PChar(′c:\autoexec.bat′), PChar(′c:\autoexec.bak′), True);
Delphi如何实现浏览文件夹,并得出所选择的文件夹名:
SelectDirectory函数(FileCtrl单元)
1、function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean; overload;
2、function SelectDirectory(var Directory: string; Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload;
注意:使用前请uses FileCtrl;
第1种调用格式示例为:
const
sCaption = '文件夹'; //弹出框标题名(非弹出框窗体名)
sRoot = ''; //初始文件夹(如'C:\','D:\DownLoad'等, 不存在则从桌面)
var
sDir: string;
begin
if SelectDirectory(sCaption, sRoot, sDir) then
//已返回所选文件夹路径给sDir,自行处理
end;
第2种调用格式示例为:
const
SELDIRHELP = 1000;
var
sDir: string; //初始文件夹(如'C:\','D:\DownLoad'等)
begin
sDir := '';
if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt], SELDIRHELP) then
//已返回所选文件夹路径给sDir,自行处理
end;
相关文章推荐
- C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名
- C++、WIN API、MFC分别遍历文件夹、获取文件名称的三种方式【耿然原创】
- iOS小技巧 - 获取指定文件夹下的所有相对路径(包括子文件夹路径),并实现指定文件夹下的所有文件的全部移动
- C++获取文件夹下所有文件名称的三种方式
- Delphi 获取指定文件的驱动器名, 文件夹名, 路径名, 文件名
- Qt5 实现文件复制并在指定路径下创建文件夹
- 配置文件的访问时的路径获取三种方式
- Tomcat项目发布的三种方式;一种:直接将项目(文件)直接复制到tomcat/webapps下二种:在tomcat/conf/server.xml配置tomcat的虚拟路径;
- c#如何实现对指定路径文件夹的遍历并复制里面所有文件到指定路径
- [代码]Delphi实现获取文件及文件夹大小(支持超过2G的大文件)
- 从指定文件夹下获取文件路径列表的实现方法
- 实现原文件和文件夹路径一起复制到另一个文件中 工具类
- WPF 获取文件夹路径,目录路径,复制文件,选择下载文件夹/目录
- 获取文件路径(Delphi )----转贴
- Java中实现复制文件或文件夹
- 枚举指定路径下文件及文件夹复制到指定路径下
- C++获取快速启动文件夹下快捷方式所指向的原路径
- javascript获取当前电脑操作系统的文件路径的方式
- 在JSP中获取当前文件,当前文件夹,当前web的绝对路径
- C#两种方式获取指定文件夹下所有子目录及文件