什么原因导致这个错误'无法写入应用程序file.ini' [英] what causes this error 'Unable to write to application file.ini'
问题描述
我的应用程序是在delphi中构建的,除了Windows 7 64bit机器之外,其他平台上运行完美。每一次尝试关闭应用程序是给我这个错误
'无法写入应用程序file.ini'
My application is build in delphi and it runs perfect on other platforms except Windows 7 64bit machine. Each and everytime try to close the application is giving me this error 'Unable to write to application file.ini'
这里是我的代码关闭
here is my code for closing
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
frmMain.close;
end;
推荐答案
此错误通常是由于尝试写入您的应用程序自己的文件夹在程序文件
下,不允许在Vista和更高版本的非管理员(和XP,如果您不以管理员或高级用户身份运行) 。
This error is usually caused by trying to write to your app's own folder under Program Files
, which is not allowed for a non-Administrator under Vista and higher (and XP, if you're not running as an Administrator or Power User).
以下是为.INI文件获取正确文件夹的一些代码:
Here's some code for getting the proper folder for your .INI file:
uses
Windows,
ShlObj; // For SHGetSpecialFolderPath
function GetFolderLocation(Handle: HWnd; Folder: Integer): string;
begin
Result := '';
SetLength(Result, MAX_PATH);
if not SHGetSpecialFolderPath(Handle, PChar(Result), Folder, False) then
RaiseLastOSError;
end;
我在我的应用程序中使用这些来检索非漫游配置文件文件夹,并使用一个子文件夹创建在我的应用程序的数据下面。在创建 TDataModule
期间设置:
I use these in my application to retrieve the non-roaming profile folder, and use a sub-folder created beneath that for my app's data. It's set up during the creation of a TDataModule
:
procedure TAppData.Create(Sender.TObject);
begin
// DataPath is a property of the datamodule, declared as a string
// CSIDL_LOCAL_APPDATA is the local non-roaming profile folder.
// CSIDL_APPDATA is for the local roaming profile folder, and is more typically used
DataPath := GetFolderLocation(Application.Handle, CSIDL_LOCAL_APPDATA);
DataPath := IncludeTrailingPathDelimiter(DataPath) + 'MyApp\';
end;
请参阅 MSDN的文档页面各种 CSIDL _
或 FOLDERID _
值。 FOLDERID _
值相似,但仅在Vista及更高版本上可用,并与 SHGetKnownFolderIDList 。
See MSDN's documentation page on the meaning of the various CSIDL_
or FOLDERID_
values. The FOLDERID_
values are similar, but are available only on Vista and above and used with SHGetKnownFolderIDList.
对于那些你不愿意忽略MS对 SHGetSpecialFolderPath
不支持的警告,这里是 GetFolderLocation
的替代版本 SHGetFolderPath
,这是首选:
For those of you not willing to disregard MS's warnings about SHGetSpecialFolderPath
not being supported, here's an alternate version of GetFolderLocation
using SHGetFolderPath
, which is preferred:
uses
ShlObj, SHFolder, ActiveX, Windows;
function GetFolderLocation(Handle: HWnd; Folder: Integer): string;
begin
Result := '';
SetLength(Result, MAX_PATH);
if not Succeeded(SHGetFolderPath(Handle, Folder, 0, 0, PChar(Result))) then
RaiseLastOSError();
end;
最后,对于只使用Vista及更高版本的用户,以下是使用 SHGetKnownFolderPath - 注意这是不可用的在Delphi的前XE版本(AFAIK - 可能在2009年或2010年)中,您将需要使用 KNOWNFOLDERID 值而不是 CSIDL _
,像 FOLDERID_LocalAppData
:
And finally, for those working with only Vista and higher, here's an example using SHGetKnownFolderPath - note this isn't available in pre-XE versions of Delphi (AFAIK-may be in 2009 or 2010), and you'll need to use KNOWNFOLDERID values instead of CSIDL_
, like FOLDERID_LocalAppData
:
uses
ShlObj, ActiveX, KnownFolders;
// Tested on XE2, VCL forms application, Win32 target, on Win7 64-bit Pro
function GetFolderLocation(const Folder: TGuid): string;
var
Buf: PWideChar;
begin
Result := '';
if Succeeded(SHGetKnownFolderPath(Folder, 0, 0, Buf)) then
begin
Result := Buf;
CoTaskMemFree(Buf);
end
else
RaiseLastOSError();
end;
这篇关于什么原因导致这个错误'无法写入应用程序file.ini'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!