is there a way to programmatically change folder permissions in windows vista?

either with mfc or .net (c++ or c# preferred)

Here's the scenario - I have an MFC dialog app that I am trying to make vista-compatible.  I need to be able to write to files in the installpath (and subdirs) at all time, so I figure that i need to give the "Everyone" account full access to the folder.  Can I do this with code, or will I need to have the end user change the permissions?

Thanks for any suggestions you can offer!!
aesdttOwnerAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Use

    LPTSTR FileName = "C:\\Temp\\MyDir";
    LPTSTR TrusteeName = "Everyone";

    DWORD AccessMask = GENREIC_READ | GENERIC_WRITE;
    ACCESS_MODE option = GRANT_ACCESS;
    DWORD InheritFlag = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE;    
    EXPLICIT_ACCESS explicitaccess;


    PACL ExistingDacl;
    PACL NewAcl = NULL;
    PSECURITY_DESCRIPTOR psd = NULL;

    DWORD dwError;

    dwError = GetNamedSecurityInfo(
                        FileName,
                        SE_FILE_OBJECT,
                        DACL_SECURITY_INFORMATION,
                        NULL,
                        NULL,
                        &ExistingDacl,
                        NULL,
                        &psd
                        );

    BuildExplicitAccessWithName(
            &explicitaccess,
            TrusteeName,
            AccessMask,
            option,
            InheritFlag
            );

    //
    // add specified access to the object
    //

    dwError = SetEntriesInAcl(
            1,
            &explicitaccess,
            ExistingDacl,
            &NewAcl
            );

    //
    // apply new security to file
    //

    dwError = SetNamedSecurityInfo(
                    FileName,
                    SE_FILE_OBJECT, // object type
                    DACL_SECURITY_INFORMATION,
                    NULL,
                    NULL,
                    NewAcl,
                    NULL
                    );

(This is a stripped down version of the MS' AclAPI sample)
0
 
aesdttOwnerAuthor Commented:
sorry it took so long to respond - i actually was able to get this all done in c++, but you deserve the points anyway.  if anyone ever needs some help with this in c++, let me know
0
 
VapiSoftCommented:
I implemented this code on Vista and it worked well.
But when I tried to set it from the VISTA to a remote folder on an XP computer
\\remotexp\c\folder
It recived an error ACCESS_DENIED (5)
0
 
jkrCommented:
Sorry, you can't do that remotely this way that easily.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.