AssignFile1

This example uses a file listbox and a regular list box on a form. The following routine scans through the files listed in the file list box and lists the sizes of any selected files to the regular list box. To exercise the error condition, create a file in the Debug directory, start this application, and then delete the file. Now try to list the size of the deleted file. Set the MultiSelect and ExtendedSelect properties on the FileListBox.

procedure TForm1.Button1Click(Sender: TObject);

var

F: File;

i, filehandle: Integer;

begin

for i:= 0 to (FileListBox1.Items.Count - 1) do begin

try

if FileListBox1.Selected[i] then

begin

if not FileExists(FileListBox1.Items.Strings[i]) then

begin

MessageDlg('File: ' + FileListBox1.Items.Strings[i] +

' not found', mtError, [mbOk], 0);

Continue;

end;

filehandle:= FileOpen(FileListBox1.Items.Strings[i], fmOpenWrite);

if (filehandle = -1) then

begin

MessageDlg('File: ' + FileListBox1.Items.Strings[i] +

' cannot be opened with access mode fmOpenWrite.', mtError, [mbOk], 0);

Continue;

end

else

FileClose(filehandle);

AssignFile(F, FileListBox1.Items.Strings[i]);

Reset(F, 1);

ListBox1.Items.Add(

FileListBox1.Items.Strings[i] + ': ' + IntToStr(FileSize(F)));

CloseFile(F);

end;

finally

{ Do something here. }

end;

end;

end;

AssignFile2

Use a TOpenDialog to select a file. Place the first line of the file into a TEdit. Click the text edit to open the dialog box.

procedure TForm1.Edit1Click(Sender: TObject);var F: TextFile; S: string;begin if OpenDialog1.Execute then { Display Open dialog box. } begin AssignFile(F, OpenDialog1.FileName); { File selected in the dialog } Reset(F); Readln(F, S); { Read the first line of the file } Edit1.Text:= S; { Put string in a TEdit control. } CloseFile(F); end;end;

System.IOUtils

TFile

AppendAllText Добавляет текст в файл. Используйте AppendAllText, чтобы добавить текст в файл. Если файл, указанный в параметре Path существует, текст добавляет к нему, в противном случае файл создается и заполняется текстом.
Copy Копирование файла по указанному пути. Используйте Copy, чтобы сделать копию файла. Первая форма Copy принимает только исходный и конечный пути. Если конечный путь указывает на уже существующий файл, Copy вызывает исключение. Вторая форма Copy принимает необязательный параметр Overwrite. Если установлено значение True, параметр заставляет операцию копирования продолжать, даже если файл назначения уже существует.
Create Создает новый файл и возвращает поток, связанный с этим файлом. Используйте Create, чтобы создать новый файл и получить экземпляр TFileStream. Create создает новый файл по указанному пути, а затем создает TFileStream связанные с этим файлом.
Delete Удаляет файл
Exists Существует ли файл?
GetAttributes Получить атрибуты

Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: