/// <summary>Adiciona permissões (Todos + FullControl) na pasta passada por parâmetro</summary>
 /// <param name="fullPath">string fullPath</param>
 /// <returns>Boolean</returns>
 private bool GrantAccess(string fullPath)
 {
     try
     {
         DirectoryInfo     dInfo     = new DirectoryInfo(fullPath);
         DirectorySecurity dSecurity = dInfo.GetAccessControl();
         Log.Log("Adicionando permissões a pasta Network " + dSecurity.ToString());
         dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, AccessControlType.Allow));
         //dInfo.SetAccessControl(dSecurity); demora muito se feito pela rede
         Log.Log("Permissões adicionadas em : " + fullPath);
         return(true);
     }
     catch (Exception ex)
     {
         MessageBox.Show("" + ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.Log("Erro ao atribuir permissões a pasta Network em: " + fullPath);
         return(false);
     }
 }
        public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
        {
            // TODO: Insert monitoring activities here.
            eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++);
            string caminho = @"D:\inetpub\";

            foreach (string pastaCliente in Directory.GetDirectories(caminho))
            {
                foreach (string pastasInterna in Directory.GetDirectories(pastaCliente))
                {
                    if (pastasInterna.Remove(0, pastaCliente.Length).ToLower() == @"\imagens")
                    {
                        // Dar permissão a pasta
                        // Create a new DirectoryInfo object.
                        DirectoryInfo dInfo = new DirectoryInfo(pastasInterna);

                        DirectorySecurity oDirSec = Directory.GetAccessControl(pastasInterna);

                        // Define o usuário Everyone (Todos)
                        SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                        //SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
                        NTAccount oAccount = sid.Translate(typeof(NTAccount)) as NTAccount;

                        oDirSec.PurgeAccessRules(oAccount);

                        FileSystemAccessRule fsAR = new FileSystemAccessRule("IIS_IUSRS",
                                                                             FileSystemRights.Modify,
                                                                             InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                             PropagationFlags.None,
                                                                             AccessControlType.Allow);

                        // Atribui a regra de acesso alterada
                        oDirSec.SetAccessRule(fsAR);
                        Directory.SetAccessControl(pastasInterna, oDirSec);

                        // USUARIO IUSR
                        fsAR = new FileSystemAccessRule("IUSR",
                                                        FileSystemRights.Modify,
                                                        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                        PropagationFlags.None,
                                                        AccessControlType.Allow);

                        // Atribui a regra de acesso alterada
                        oDirSec.SetAccessRule(fsAR);
                        Directory.SetAccessControl(pastasInterna, oDirSec);

                        eventLog1.WriteEntry(oDirSec.ToString(), EventLogEntryType.Information);

                        /*
                         * // Get a DirectorySecurity object that represents the
                         * // current security settings.
                         * DirectorySecurity dSecurity = dInfo.GetAccessControl();
                         *
                         * // Add the FileSystemAccessRule to the security settings.
                         * dSecurity.AddAccessRule(new FileSystemAccessRule(@"WIN10-EWERTON\Usuários",
                         *                                              FileSystemRights.FullControl,
                         *                                              AccessControlType.Allow));
                         *
                         */

                        eventLog1.WriteEntry(pastasInterna.ToString() + " adicionado direito", EventLogEntryType.Information);
                    }
                    else
                    {
                        eventLog1.WriteEntry(pastasInterna.Remove(0, pastaCliente.Length).ToLower() + " Não é pasta imagem", EventLogEntryType.Information);
                    }
                }
            }
        }