Link to home
Start Free TrialLog in
Avatar of farekat
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\MyProduct 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
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BluePain
BluePain

You can also use the terminal "attrib".
system("attrib -your command-");
>>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',
Avatar of farekat

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 ("BuildExplicitAccessWithName")

http://msdn2.microsoft.com/en-us/library/aa379576.aspx ("SetEntriesInAcl")
Avatar of farekat

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?
Avatar of farekat

ASKER

I was not getting any errors but it just didnt set the permissions for the directory.  Ill submit code shortly.
Avatar of farekat

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?