私は Delphi 7 を使用しており、特定のディレクトリ内のすべてのファイルを文字列グリッド (行ごとに 1 つのファイル、すべて 1 列に) に一覧表示したいと考えています。私は今約1時間検索しましたが、これを行う方法の例が見つからないので、あなたが提供できる助けがあれば幸いです.
質問する
15010 次
2 に答える
7
TStrings
これにより、子孫(たとえばTStringList
、、TMemo.Lihes
など)が指定されたフォルダ内のすべてのファイルでいっぱいになります。
function GetFiles(const StartDir: String; const List: TStrings): Boolean;
var
SRec: TSearchRec;
Res: Integer;
begin
if not Assigned(List) then
begin
Result := False;
Exit;
end;
Res := FindFirst(StartDir + '*.*', faAnyfile, SRec );
if Res = 0 then
try
while res = 0 do
begin
if (SRec.Attr and faDirectory <> faDirectory) then
// If you want filename only, remove "StartDir +"
// from next line
List.Add( StartDir + SRec.Name );
Res := FindNext(SRec);
end;
finally
FindClose(SRec)
end;
Result := (List.Count > 0);
end;
このように使用して、データを入力しますTStringGrid
(Grid
以下のコードでは、最長のファイル名の長さに基づいて列のサイズを自動化するコードを追加しました)。
var
SL: TStringList;
i: Integer;
MaxWidth, CurrWidth: Integer;
const
Padding = 10;
begin
SL := TStringList.Create;
try
if GetFiles('C:\Temp\', SL) then
begin
MaxWidth := Grid.ColWidths[0];
for i := 0 to SL.Count - 1 do
begin
CurrWidth := Grid.Canvas.TextWidth(SL[i]);
if CurrWidth > MaxWidth then
MaxWidth := CurrWidth;
// Populates first column in stringgrid.
Grid.RowCount := Grid.RowCount + 1;
Grid.Cells[0, Grid.RowCount - 1] := SL[i];
end;
Grid.ColWidths[0] := MaxWidth + Padding;
end;
finally
SL.Free;
end;
end;
このコードでは、パスの後にフォルダー名の後にバックスラッシュを含める必要があることに注意してください。簡単に変更して、必要に応じて自動的に追加したり、フォルダ名とファイルマスクの両方を受け入れて特定のファイルのみを含めることができます。
于 2012-07-15T05:57:59.683 に答える
0
SysUtil.FindFirst/FindNext/FindCloseを使用してファイルを取得する
目的の行/列に文字列を挿入するだけです。必要に応じて「RowCount」を増やします。
于 2012-07-15T05:36:45.440 に答える