Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
first i created installer class like this

C#
[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);
            SetDirectorySecurity();
        }

        private void SetDirectorySecurity()
        {
            string targetdir = Path.GetDirectoryName(Context.Parameters["Masterfinal"]);
            Log("Adding modify rights for " + targetdir);
            // The following code was copied (and modified) from "Jaso" (http://www.aspnet-answers.com/microsoft/NET-Security/30001760/how-to-change-group-permissions-on-existing-folder.aspx).
            // Retrieve the Directory Security descriptor for the directory
            var dSecurity = Directory.GetAccessControl(targetdir, AccessControlSections.Access);
            // Build a temp domainSID using the Null SID passed in as a SDDL string.
            // The constructor will accept the traditional notation or the SDDL notation interchangeably.
            var domainSid = new SecurityIdentifier("S-1-0-0");
            // Create a security Identifier for the BuiltinUsers Group to be passed to the new accessrule
            var ident = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, domainSid);
            // Create a new Access Rule.
            // ContainerInherit AND ObjectInherit covers both target folder, child folder and child object.
            // However, when using both (combined with AND), the permissions aren't applied.
            // So use two rules.
            // Propagate.none means child and grandchild objects inherit.
            var accessRule1 = new FileSystemAccessRule(ident, FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
            var accessRule2 = new FileSystemAccessRule(ident, FileSystemRights.Modify, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
            // Add the access rules to the Directory Security Descriptor
            dSecurity.AddAccessRule(accessRule1);
            dSecurity.AddAccessRule(accessRule2);
            // Persist the Directory Security Descriptor to the directory
            Directory.SetAccessControl(targetdir, dSecurity);
            Log("Rights added.");
        }

        private void Log(string message)
        {
            Context.LogMessage(message);
            System.Diagnostics.Trace.WriteLine(message, "1177 client installer");
        }

    }
}



mysetupfile->view->customactions->install->add output of installer project

when i am installing my project i got a error "1001 parameter null exception"

where am doing wrong?


what is custom action property?
Posted
Updated 25-Nov-13 21:53pm
v3

I am using the below Method to apply permission


C#
private bool SetPermission(string applyPath)
        {
            try
            {
                DirectoryInfo IsRootAvailable = new DirectoryInfo(applyPath);
                if (IsRootAvailable.Exists)
                {
                    WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                    FileSystemRights Rights = (FileSystemRights)0;
                    Rights = FileSystemRights.Modify;

                    // *** Add Access Rule to the actual directory itself
                    FileSystemAccessRule AccessRule = new FileSystemAccessRule("NETWORK SERVICE", Rights,
                                                InheritanceFlags.None,
                                                PropagationFlags.NoPropagateInherit,
                                                AccessControlType.Allow);
                    DirectoryInfo Info = new DirectoryInfo(applyPath);
                    DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access);

                    bool Result = false;
                    Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);
                    if (!Result)
                    {
                        rtxtConfirmMessage.Text += string.Format("\t\t\t--\t{0}", "Failed.");
                        return false;
                    }
                    // *** Always allow objects to inherit on a directory
                    InheritanceFlags iFlags = InheritanceFlags.ObjectInherit;
                    iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

                    // *** Add Access rule for the inheritance
                    AccessRule = new FileSystemAccessRule("NETWORK SERVICE", Rights,
                                                iFlags,
                                                PropagationFlags.InheritOnly,
                                                AccessControlType.Allow);
                    Result = false;
                    Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);
                    if (!Result)
                    {
                        rtxtConfirmMessage.Text += string.Format("\t\t\t--\t{0}", "Failed.");
                        return false;
                    }
                    Info.SetAccessControl(Security);
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                rtxtConfirmMessage.Text += string.Format("\t\t\t--\t{0}", "Failed." + ex.Message);
                return false;
            }
        }


and a help from MSDN FileSystemAccessRule Class[^] has the below code

C#
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "test.xml";

                Console.WriteLine("Adding access control entry for "
                    + fileName);

                // Add the access control entry to the file.
                AddFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from "
                    + fileName);

                // Remove the access control entry from the file.
                RemoveFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {


            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }
    }
}
 
Share this answer
 
v2
Comments
Raajkumar.b 28-Nov-13 2:49am    
hi
after installed project ,above code is works only for my system
when i run same setup in another system it doesn't work

for that i changed your code
string accname = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
FileSystemAccessRule AccessRule = new FileSystemAccessRule(accname ,.....)
now also it doesn't work
any idea?
[no name] 29-Nov-13 4:26am    
I am getting any issue with my code. i have tried it even in VM ware's it working fine. may be it will be related security crediential. let me know what error you getting put some try catch block and pop up a message box and get know if it throwing any error or something like that. hope you can debug so put some message box so that you will know if the code is getting executed. so you will get som idea or spot what was the error.

Let me know if you have got solution or identifited the problem
Raajkumar.b 30-Nov-13 0:46am    
hithanks for ur reply ,it shows "Operation must use updateble query"
(i am using windows 7 o.s and inastall project in c:drive)
[no name] 1-Dec-13 21:56pm    
hope this link will help you http://support.microsoft.com/kb/175168 as for as i knew you must be using some SQL query inside your install program. so please get to know where the exact error is.

probable reason:
1. you would have pointed to your local SQL server (" have a look in to the SQL Crediential in the SQL Script") in the sql script.
2. obviously Any crediential's that might be pointing to your local Environment. go throught your code base if you found anything related to this.

Please let me know if you have any issues.
Raajkumar.b 1-Dec-13 23:32pm    
hi,
i am using text files as database(oledb conncetion)
 
Share this answer
 
Comments
Raajkumar.b 26-Nov-13 0:11am    
i want how to add above installer class output to set up file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900