1

So I made an application that fills bookmarks in a .doc file with textbox text. Everything was working fine when I was developing it.

Now that I am trying to debug it after an installation process, I get a permission error that is solved, if i manually set the folders in which the templates are to full access to the local user.

Those files are sorted like so :

ProgramFiles

     DefaultCompanyName

          ApplicationName

               WordTemplateLocalation#1

               WordTemplateLocalation#2

I want to give modify and write permissions to those folders and their child (meaning the templates and other resources in those folders).

I've tried many things i've found in here StackOverflow and in MSDN, or any other blogs, but i can't seem to understand quite exactly how to create a Custom Install Action.

Here's what I followed mostly : https://stackoverflow.com/a/10540927/6898247

But it doesn't seem to work even with this : https://stackoverflow.com/a/4392394/433718

And even is I've followed MSDN walkthrough : https://msdn.microsoft.com/en-us/library/d9k65z2d(v=vs.100).aspx

I've tried to add the DLL from the walkthrough, but nothing changed.

I'm not quite sure where i do something wrong.

What am I supposed to do once I've created a Class Installer like this :

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Security.Principal;
using System.Security.AccessControl;

namespace Permissions_Setter
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            base.Install(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);
        }
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
Bo. Ga.
  • 138
  • 1
  • 4
  • 12
  • 5
    This is a pretty much discouraged practice to put modifiable data files into application's folder inside Program Files. You should use appropriate folders in %appdata% or %localappdata% instead. – rs232 Nov 16 '17 at 09:48
  • i'm dumb, i haven't even thought about it.. Trying now. – Bo. Ga. Nov 16 '17 at 09:53

0 Answers0