【问题标题】:Saving record to file Error 'file access denied'将记录保存到文件错误“文件访问被拒绝”
【发布时间】:2014-10-27 15:24:17
【问题描述】:

当我运行我的代码时,选择我创建的保存按钮。记录未保存,但我收到错误“文件访问被拒绝”。

我的代码:

我将代码拆分为 2 个单元 MainUnit 和 AddTenantUnit。

我认为问题出在代码末尾的过程中。如果您向下滚动,我会明确说明是哪个程序 (TAddTenantForm.SaveButtonClick)。

unit MainUnit;

interface

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

type
  TMainForm = class(TForm)
    AddTenantButton: TButton;
    procedure FormCreate(Sender: TObject);
    procedure AddTenantButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  TTenantRecord = record
    FirstName : string[20];
    LastName : string[20];
  end;

var
  MainForm: TMainForm;
  Tenant : TTenantRecord;
  TenantFile : file of TTenantRecord;

implementation

uses AddTenantUnit;

{$R *.dfm}

procedure TMainForm.AddTenantButtonClick(Sender: TObject);
begin
  AddTenantForm.ShowModal;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  assignfile (TenantFile, 'Tenant.dat');
  if not fileexists ('Tenant.dat')
  then
    begin
      rewrite (TenantFile);
      closefile (TenantFile)
    end
  {endif};
end;

end.


unit AddTenantUnit;

interface

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

type
  TAddTenantForm = class(TForm)
    MainFormButton: TButton;
    FirstNameLabel: TLabel;
    FirstNameEdit: TEdit;
    LastNameLabel: TLabel;
    LastNameEdit: TEdit;
    SaveButton: TButton;
    ClearButton: TButton;
    procedure SaveButtonClick(Sender: TObject);
    procedure LastNameEditChange(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
    procedure FirstNameEditChange(Sender: TObject);
    procedure MainFormButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  AddTenantForm: TAddTenantForm;

implementation

{$R *.dfm}

procedure TAddTenantForm.MainFormButtonClick(Sender: TObject);
begin
  AddTenantForm.Close;
end;

procedure TAddTenantForm.FirstNameEditChange(Sender: TObject);
begin
  Tenant.FirstName := FirstNameEdit.Text;
end;

procedure TAddTenantForm.ClearButtonClick(Sender: TObject);
begin
  FirstNameEdit.Clear;
  LastNameEdit.Clear;
end;

procedure TAddTenantForm.LastNameEditChange(Sender: TObject);
begin
  Tenant.LastName := LastNameEdit.Text;
end;

// This is where the problem lies when I run this piece of
// code. This represents the Save button being clicked.
procedure TAddTenantForm.SaveButtonClick(Sender: TObject);
begin
  assignfile (TenantFile, 'Tenant.dat');
  write(TenantFile, Tenant);
  closefile (TenantFile);
end;


end.

【问题讨论】:

  • 我看不到路径前缀。应用尝试写入哪个文件夹(name!)?
  • 您没有在尝试写入Tenant.dat 的代码块中指定文件夹位置。您尝试写入的文件在哪里? (如果你不能回答,那肯定是位置不对。)
  • 您是否在启用 UAC 的情况下运行?

标签: delphi


【解决方案1】:

您正在尝试将数据写入未打开的文件。

procedure TAddTenantForm.SaveButtonClick(Sender: TObject);
begin
  assignfile (TenantFile, 'Tenant.dat');
  // Rewrite(TenantFile) or Reset(TenantFile) missed here
  write(TenantFile, Tenant);
  closefile (TenantFile);
end;

【讨论】:

    猜你喜欢
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多