将值从Delphi保存到磁盘:"保存"和"另存为"

时间:2014-03-16 23:08:45

标签: delphi delphi-2010 lazarus

我正在创建一个程序,使用以下代码在磁盘上保存一些信息(通常是编辑字段):

  procedure TForm1.ToolButton3Click(Sender: TObject);   
    begin    
    if  Savedialog1.Execute then
         begin
            AssignFile(myFile, save.filename);
            ReWrite(myFile);
            customer.Name:=name.text;
            customer.LastName:= LastName.Text;
           //[...] <-- much more fields
            Write(myFile, customer);
            CloseFile(myFile);  
            end;

end;

这个代码打开:

procedure TForm1.ToolButton12Click(Sender: TObject);
begin
  if opendlg.execute then begin
     Form1.Caption := 'Program Name '+' - '+extractfilename(Opendlg.FileName);

     if FileExists(opendlg.filename) then
    begin
     AssignFile(myfile, opendlg.filename);

    end;
    // Reopen the file in read only mode
   FileMode := fmOpenRead;
   Reset(myFile);

   // Display the file contents
   while not Eof(myFile) do
   begin
     Read(myFile, customer);

   Name.text:=customer.Name;
   LastName.Text:=customer.LastName; 
   //[...] <-- much more fields
end;
end;
end;   

我不知道该方法是否更准确,但它是我正在使用的=(。那么如何点击保存按钮我只需更新以前保存的文件而不需要重新打开保存对话框?谢谢!

1 个答案:

答案 0 :(得分:0)

像这样:

procedure TForm1.ToolButton3Click(Sender: TObject);   
begin    
  if SaveDialog1.FileName = '' then
  begin
    if not SaveDialog1.Execute then
      Exit;
    end;
  end;
  AssignFile(myFile, SaveDialog1.FileName);
  ...
end;