首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

这个代码怎么改?关于取得文件夹下的文件清单

2013-11-05 
这个代码如何改?--------------关于取得文件夹下的文件清单?路过的大虾帮看一下,偶要取得文件夹下文件,不

这个代码如何改?--------------关于取得文件夹下的文件清单?
路过的大虾帮看一下,
偶要取得文件夹下文件,不包括子文件夹
请问如何改?
1.取得所有文件的清单,为什么我的代码没东西出来?
2,取得BMP,JPE,这两种文件的清单,如何改代码?

谢了.


//调用方法:
DirFileList:=TStringList.Create;
FindFile(self,Trim(Edit1.Text),'.*',DirFileList);


//函数取得文件夹下文件,不包括子文件夹
function FindFile(Form1:TForm;Path,Ext :string;var sl :TStringList):Boolean;
const
    DIRATTR = DDL_READWRITE or DDL_READONLY or DDL_HIDDEN or DDL_ARCHIVE;
var
  Files :string;
  lb :TListBox;
begin
  Result := False;
  lb := TListBox.Create(Form1);
  try
    if Path[Length(Path)] <> '\' then
      Path := Path + '\';
    if DirectoryExists(Path) then
    begin
      lb.Visible := false;
      lb.Parent := Form1;
      Files := Ext;
      SetCurrentDirectory(PChar(Path));
      SendMessage(lb.Handle, LB_DIR, DIRATTR, LParam(Files));
      sl.AddStrings(lb.Items);
      Result := True;
    end;
  except
  
  end;
  FreeAndNil(lb);
end;

[解决办法]

FileList:=TStringList.Create;
procedure GetFilesName(sDirectory: String);
var
  i:Integer;
  sr:TSearchRec;
  sPath,sFile:   String;
begin
  i:=0;
  if Copy(sDirectory,Length(sDirectory),1)<>'\' then
      sPath:=sDirectory+'\'
  else
      sPath:=sDirectory;
  try
    if SysUtils.FindFirst(sPath+'*.dll',faAnyFile, sr)=0   then
    begin
        repeat
            sFile:=Trim(sr.Name);
            if   sFile='.'   then   Continue;
            if   sFile='..'   then   Continue;
            sFile:=sPath+sr.Name;
            FilesList.Add(IntToStr(i)+'='+sFile);
            inc(i);
        until   SysUtils.FindNext(sr)<>0;
         SysUtils.FindClose(sr);
    end;
  except
    on E: Exception do
      begin
        showMessage(E.Message);
      end;
  end;
end;


你试试看吧

热点排行