farekat
asked on
Set File Permissions via Code
Can someone tell me how to set file permissions in Delphi? I am using TINIFile.Create to create INI files in my application. The problem is that if I create the file while logged onto Windows as an administrator and then try to run the application as just a standard user and overwrite the INI file, I do not have permission to do so. I put the file in the AllUsers\ApplicationData\M yProduct folder. I'd like to either set the permissions to this folder or to the INI file I create. I need AllUsers to have Full permissions to either the MyProduct folder or to the INI file I create. If its possible to do this through a WindowsAPI that'd be great because I also need to do this in C++ and C#. I'd really appreciate any help. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>You can also use the terminal "attrib".
No - 'attrib' manages attributes, not permissions. 'cacls.exe' manages permissions in the above way, but that does not meet the criteria 'via code',
No - 'attrib' manages attributes, not permissions. 'cacls.exe' manages permissions in the above way, but that does not meet the criteria 'via code',
ASKER
thanks jkr, do I have to include any special uses (i.e. System, Windows) in the Delphi file...to use GetNamedSecurityInfo...etc ??
I see
I don't know Delphi very well, but you'll need advapi32.dll (aclapi.h). Just check the MSDN docs for the respective functions:
http://msdn2.microsoft.com/en-us/library/aa379579.aspx ("SetNamedSecurityInfo")
http://msdn2.microsoft.com/en-us/library/aa376378.aspx ("BuildExplicitAccessWithN ame")
http://msdn2.microsoft.com/en-us/library/aa379576.aspx ("SetEntriesInAcl")
http://msdn2.microsoft.com/en-us/library/aa379579.aspx ("SetNamedSecurityInfo")
http://msdn2.microsoft.com/en-us/library/aa376378.aspx ("BuildExplicitAccessWithN
http://msdn2.microsoft.com/en-us/library/aa379576.aspx ("SetEntriesInAcl")
ASKER
It works when I do it to a file but I cant seem to get it to work with just a folder...any ideas?
Works with a folder also. In fact the code was even used for one. What error codes do you get?
ASKER
I was not getting any errors but it just didnt set the permissions for the directory. Ill submit code shortly.
ASKER
I just got it to work NEVERMIND!!!! what a nightmare, JKR you're the best thanks for the help!
farekat:
Would you mind sharing your Delphi code with me which sets permission to the folder?
Would you mind sharing your Delphi code with me which sets permission to the folder?
system("attrib -your command-");