How to give Read/Write permissions to a Folder during installation using .NET

后端 未结 9 1513
不思量自难忘°
不思量自难忘° 2020-11-27 14:33

I have a Setup project that I have build using Visual Studio 2010.

The installer works fine in terms of installing the application and all its dependencies into thei

相关标签:
9条回答
  • 2020-11-27 15:05

    I guess my other post was deleted for being a little too general, so I've refined it below:

    The thing to do is make a custom action. It's pretty straightforward, check out the MSDN walkthrough for writing a C# custom action here. You'll put your permission-changing code inside the Install method:

    Follow the first few steps from the link to get a new installer project referenced from your installer solution. You have to do it this way, so you can build a dll that is called at the end of installation.

    Actually setting read/write privileges for Users was a little trickier, and the closest I could get was to set for Authenticated Users. I cobbled together a few other solutions I found on the Internet to come up with this:

    public override void Install(IDictionary stateSaver)
    {
        // This gets the named parameters passed in from your custom action
        string folder = Context.Parameters["folder"];
    
        // This gets the "Authenticated Users" group, no matter what it's called
        SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
    
        // Create the rules
        FileSystemAccessRule writerule = new FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow);
    
        if (!string.IsNullOrEmpty(folder) && Directory.Exists(folder))
        {
            // Get your file's ACL
            DirectorySecurity fsecurity = Directory.GetAccessControl(folder);
    
            // Add the new rule to the ACL
            fsecurity.AddAccessRule(writerule);
    
            // Set the ACL back to the file
            Directory.SetAccessControl(folder, fsecurity);
        }
    
        // Explicitly call the overriden method to properly return control to the installer
        base.Install(stateSaver);
    }
    

    Then, when you create your custom action, edit its properties, and add something like this under the CustomActionData property:

    /folder="[CommonAppDataFolder][ProductName]"
    
    0 讨论(0)
  • 2020-11-27 15:07

    The behavior is by design. The programs should not be modifying themselves (hence their installation directory) for anything but updates (which again can be done with Windows installer without a problem). If you're using .NET, isolated storage is an excellent location to store user data.

    0 讨论(0)
  • 2020-11-27 15:11
    DirectoryInfo info = new DirectoryInfo(path[x]);
    
    DirectorySecurity security = info.GetAccessControl();
    
    security.AddAccessRule(new FileSystemAccessRule(logonName, FileSystemRights.Modify, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
    
    security.AddAccessRule(new FileSystemAccessRule(logonName, FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
    
    info.SetAccessControl(security); 
    

    Setting the inherit part is also important if you want to save and access more than just one file in the ProgramData folder.

    0 讨论(0)
  • 2020-11-27 15:11

    Change the defaultLocation to: C:[Manufacturer][ProductName] you can change the drive C with whatever drive you like. See this picture

    0 讨论(0)
  • 2020-11-27 15:12

    By default Users group doesn't have write access in per-machine locations like Program Files. This is a Windows standard which is not related to installations. However, during install you can set any permissions you want.

    Windows Installer does support custom permissions, but Visual Studio doesn't offer a way for setting them. So the only solution in Visual Studio is a custom action.

    Unfortunately Visual Studio doesn't support attached custom actions. So using XCACLS.EXE to set permissions would work only if you include it in your package (it will be installed on the target machine along with your files).

    A cleaner, but more complex solution is to write a custom action yourself (using custom code) to set the permissions you want.

    The fastest and cleanest solution would be to use a different setup authoring tool which offers more control over permissions.

    0 讨论(0)
  • 2020-11-27 15:20

    I put the folders I want to copy in a folder in the Application Folder section of the Setup Wizard which is in the C:\Program Files (x86) directory.

    Then when the program runs it checks if the folders needed are in the right place, and if not copies them to the correct directory.

    So the code would be:

    If Not My.Computer.FileSystem.DirectoryExists("directory") And My.Computer.FileSystem.DirectoryExists("directory") Then
        My.Computer.FileSystem.CopyDirectory("C:\Program Files (x86)\APPFOLDER", "C:\ProgramData\APPFOLDER")
    Else
    End If
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题