天下网吧-网盟论坛【网吧程序天地】〖Delphi讨论区〗 → [原创]delphi实现新增删除文件及目录(附源码)
关闭 帖子评论
选取类型: 中立 支持 反对
观点标题:
验证码:验证码,看不清楚?请点击刷新验证码
观点内容:
(不支持HTML)
  1. 请以客观、真实地作出评论,并注意语言文明;
  2. 观点发表后不能作出更改;
回复贴子
您是本帖的第 1201 个阅读者
树形打印
标题:[原创]delphi实现新增删除文件及目录(附源码)
ainiyang
帅哥哟,离线,有人找我吗?
等级:新手上路
文章:26
积分:369
注册:2007年3月18日
发贴心情
[原创]delphi实现新增删除文件及目录(附源码)
添加到QQ书签收藏

{filelistbox和directorylistbox组件在win3.1 下}


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FileCtrl, StdCtrls;

type
  TForm1 = class(TForm)
    ED_PATH1: TEdit;
    ED_PATH2: TEdit;
    Button1: TButton;
    ED_FILE: TEdit;
    Button2: TButton;
    Button3: TButton;
    ED_OLDFILE: TEdit;
    ED_NEWFILE: TEdit;
    Button4: TButton;
    ED_DIR: TEdit;
    Button5: TButton;
    Button6: TButton;
    ED_OLDDIR: TEdit;
    ED_NEWDIR: TEdit;
    Button7: TButton;
    FileListBox1: TFileListBox;
    DirectoryListBox1: TDirectoryListBox;
    Button8: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FileListBox1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure DirectoryListBox1Change(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var

  Form1: TForm1;


implementation

{$R *.dfm}


//找出程序所在的目录
procedure TForm1.Button1Click(Sender: TObject);
begin
ed_path1.Text:=extractfilepath(application.ExeName);   //extractfilepath(application.exename)目前执行文件的所在路径
ed_path2.text:=sysutils.GetCurrentDir;  //目前磁盘驱动器所在的路径

filelistbox1.Directory:=extractfilepath(application.ExeName);  //让filelistbox里面显示执行程序所在文件夹的的所有文件夹
filelistbox1.Update;        //更新filelistbox

directorylistbox1.Directory:=extractfilepath(application.exeName);   //让directorylistbox显示执行程序所在的目录
directorylistbox1.Update;      //更新directorylistbox
end;


//单击filelistbox时,将filelistbox中的文件名放在edit中
procedure TForm1.FileListBox1Change(Sender: TObject);

begin
 if filelistbox1.ItemIndex >=0 then  //些条件是防止数据出错.因为index不能小于0小于0程序会报错
 begin
ed_file.Text:=filelistbox1.Items.Strings[filelistbox1.itemindex] ;
ED_oldfile.text:=filelistbox1.Items.Strings[filelistbox1.ItemIndex];
end;
end;

//新增文件
procedure TForm1.Button2Click(Sender: TObject);
var fileName:string;
    fileHandle:integer;
begin
    filename:=ED_file.Text;
    if fileExists(FileName) then  //检查文件是否存在如果不存在,则用filecreate()来创建
    begin
    showmessage('该文件已经存在!');
    end else begin
    fileHandle:=filecreate(filename); //实际上这里用filecreate(filiename)就可以了.但是为什么要赋值给filehandle
    //希望admin帮我解释下,我也是不太明白.
    fileclose(filehandle);
    end;
    filelistbox1.Update;
end;
 //删除文件
procedure TForm1.Button3Click(Sender: TObject);
var fileName:string;
begin
    filename:=ed_file.text;
    if fileexists(filename) then
    begin
    deletefile(filename);  //文件ed_file.text显示的文件
    end else begin
    showmessage('该文件不存在!');
    end;
   filelistbox1.Update;
   end;
//删除文件.删除filelistbox1里面选中的文件
procedure TForm1.Button8Click(Sender: TObject);
var filename:string;
begin
   if filelistbox1.ItemIndex<0 then   //防止数据出错.   filelistbox1.ItemIndex不能小于0
   begin
   showmessage('请选择一个文件');
   exit;
   end;

   filename:=filelistbox1.Items.strings[filelistbox1.itemindex];
   if fileexists(filename) then
   begin
   deletefile(filename);
   end else begin
   showmessage('该文件不存在!');
   end;
   filelistbox1.Update;

end;
 //  更改文件明
procedure TForm1.Button4Click(Sender: TObject);
 var old_filename,new_filename:string;
begin
  old_filename:=ED_OLDFILE.Text;
  new_filename:=ED_newfile.Text;
  if fileexists(OLD_filename) then       //检查old_filename是否存在,如果是,则必为new_filename
  begin
  sysutils.RenameFile(OLD_FILENAME,NEW_FILENAME);
   //sysutils是一个单元名,RenameFile这个函数是封装在SysUtils这个单元里面。
   //因为不同单元,可能会有相同名字的函数,SYSUTILS.RenameFile直接指名了使用哪个单元的什么函数,可以防止函数名的冲突。
  end else begin
  showmessage('该文件不存在啊!');
end;
  filelistbox1.Update;
  end;

  //当选择directorylistbox里面的目录时,显示到ed_dir和ed_olddir上.
procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
  ED_dir.Text:=
  directorylistbox1.Items.Strings[directorylistbox1.itemindex];
  ed_olddir.Text:=
  directorylistbox1.Items.Strings[directorylistbox1.itemindex];
end;

//新建文件夹
procedure TForm1.Button5Click(Sender: TObject);
var dirname:string;
begin
 dirname:=ED_DIR.Text;
 if directoryexists(dirname) then
 begin
  showmessage('目录已存在');
 end else begin
  createdir(dirname);
  end;
   directorylistbox1.Update;
   end;

   //删除文件夹
procedure TForm1.Button6Click(Sender: TObject);
var dirname:string;
begin
   dirname:=ED_DIR.text;
   if directoryexists(dirname) then
   begin
   removedir(dirname);
   end else begin
   showmessage('该目录不存在') ;
end;
   directorylistbox1.update;
   end;


   //更改文件夹名称
procedure TForm1.Button7Click(Sender: TObject);
var old_dirname,new_dirname:string;
begin
   old_dirname:=ed_olddir.Text;
   new_dirname:=ed_newdir.Text;

   if directoryexists(old_dirname) then
   begin
  sysutils.RenameFile(old_dirname,new_dirname);
  end else begin
  showmessage('该目录不存在');

end;
  directorylistbox1.update;
  end;
end.


图片点击可在新窗口打开查看此主题相关图片如下:1.jpg
图片点击可在新窗口打开查看

图片点击可在新窗口打开查看此主题相关图片如下:2.jpg
图片点击可在新窗口打开查看

图片点击可在新窗口打开查看此主题相关图片如下:3.jpg
图片点击可在新窗口打开查看
 下载信息(友情提示:注意杀毒,未登陆用户不能下载)
图片点击可在新窗口打开查看点击浏览该文件:6文件目录的使用.rar

[此贴子已经被作者于2008-5-6 12:04:23编辑过]



  • 版主评定:
  • 好评,获得50个金币奖励好评,获得50个金币奖励 (理由:奖励)
  • 评论[支持者: 0反对者: 0中立者: 0] 查看评论信息
2008-5-6 11:58:00
jhting
帅哥哟,离线,有人找我吗?
等级:新手上路
文章:3
积分:81
注册:2008年7月6日
发贴心情

NB呀!!!

       100多行的代码,却没有一个 try 语句....................

      不要乱发了。。现在网上**还不够多吗?


2008-7-7 18:22:00
zdxf
帅哥哟,离线,有人找我吗?
等级:论坛游民
文章:70
积分:1221
注册:2007年10月24日
发贴心情

新手看不懂,熟手又觉得太肤浅!


2008-7-15 12:46:00
lejun1984
帅哥哟,离线,有人找我吗?
等级:初来天下网吧
文章:1
积分:65
注册:2008年11月24日
发贴心情
学习一下,谢谢

2008-11-24 8:53:00


[原创]delphi实现新增删除文件及目录(附源码)
发贴表情字体颜色字体背景颜色粗体斜体下划线超级连接插入图片Flash图片realplay视频文件Media Player视频文件引用清理代码生成一个财付通交易信息显示: 预览 回复标题 上传表单
字节.