當前位置: 首頁>>代碼示例>>C#>>正文


C# FileSecurity.AddAccessRule方法代碼示例

本文整理匯總了C#中System.Security.AccessControl.FileSecurity.AddAccessRule方法的典型用法代碼示例。如果您正苦於以下問題:C# FileSecurity.AddAccessRule方法的具體用法?C# FileSecurity.AddAccessRule怎麽用?C# FileSecurity.AddAccessRule使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Security.AccessControl.FileSecurity的用法示例。


在下文中一共展示了FileSecurity.AddAccessRule方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExecuteOnFile

        protected override void ExecuteOnFile(FileInfo file)
        {
            FileSecurity fileSec = new FileSecurity(file.FullName, AccessControlSections.Access);

            Log(Level.Info, Resources.AddAccessRuleAdding, Rights, NTAccount, file.FullName);
            FileSystemAccessRule newRule = new FileSystemAccessRule(new NTAccount(NTAccount), Rights, AccessControlType);
            fileSec.AddAccessRule(newRule);
            file.SetAccessControl(fileSec);
        }
開發者ID:jcde,項目名稱:NAntWithContrib,代碼行數:9,代碼來源:AddAccessRuleTask.cs

示例2: Main

        static void Main(string[] args)
        {
            DirectorySecurity ds = new DirectorySecurity();
            ds.AddAccessRule(new FileSystemAccessRule("Rafa&Pri", FileSystemRights.Read, AccessControlType.Allow));
            string newFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Rafa&Pri");
            Directory.CreateDirectory(newFolder, ds);

            FileSecurity fs = new FileSecurity();
            fs.AddAccessRule(new FileSystemAccessRule("Rafa&Pri", FileSystemRights.FullControl, AccessControlType.Allow));
            string newFile = Path.Combine(newFolder, "Data.dat");
            File.Create(newFile, 100, FileOptions.None, fs);
        }
開發者ID:Rafael-Miceli,項目名稱:ProjectStudiesCert70-536,代碼行數:12,代碼來源:Program.cs

示例3: AllowFileAccess

 public static bool AllowFileAccess(string path, FileSystemRights rights)
 {
     try
     {
         var security = new System.Security.AccessControl.FileSecurity();
         var usersSid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
         security.AddAccessRule(new FileSystemAccessRule(usersSid, rights, AccessControlType.Allow));
         File.SetAccessControl(path, security);
         return true;
     }
     catch
     {
         return false;
     }
 }
開發者ID:Healix,項目名稱:Gw2Launcher,代碼行數:15,代碼來源:FileUtil.cs

示例4: SetAccessControl

        public void SetAccessControl()
        {
            string tempFile = Path.GetTempFileName();

            FileInfo fi = new FileInfo(tempFile);
            FileSecurity expected = fi.GetAccessControl(AccessControlSections.All);

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);
            Assert.IsNotNull(efi);

            FileSecurity fileSecurity = new FileSecurity();
            fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));

            efi.SetAccessControl(fileSecurity);

            Assert.AreNotEqual(expected.GetSecurityDescriptorBinaryForm(), efi.GetAccessControl().GetSecurityDescriptorBinaryForm());

            FileSecurity actualFileSecurity = File.GetAccessControl(tempFile);
            AuthorizationRuleCollection rules = actualFileSecurity.GetAccessRules(true, true, typeof(NTAccount));
            foreach (AuthorizationRule rule in rules)
            {
                FileSystemAccessRule accessRule = (FileSystemAccessRule)rule;

                if (accessRule.IdentityReference.Value == "Everyone")
                {
                    Assert.IsTrue(accessRule.AccessControlType == AccessControlType.Allow);
                    Assert.IsTrue(accessRule.FileSystemRights == FileSystemRights.FullControl);
                }
            }

            fi.SetAccessControl(expected);
        }
開發者ID:vip32,項目名稱:cadru,代碼行數:32,代碼來源:ExtendedFileInfoTests.cs

示例5: GetSecureFileStream

    public static FileStream GetSecureFileStream(string path, int bufferSize, FileOptions options)
    {
      if (path == null)
        throw new ArgumentNullException("path");

      if (bufferSize <= 0)
        throw new ArgumentOutOfRangeException("bufferSize");

      if ((options &
           ~(FileOptions.Asynchronous | FileOptions.DeleteOnClose | FileOptions.Encrypted | FileOptions.RandomAccess |
             FileOptions.SequentialScan | FileOptions.WriteThrough)) != FileOptions.None)
        throw new ArgumentOutOfRangeException("options");

      new FileIOPermission(FileIOPermissionAccess.Write, path).Demand();

      SecurityIdentifier user = WindowsIdentity.GetCurrent().User;
      FileSecurity fileSecurity = new FileSecurity();
      fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
      fileSecurity.SetAccessRuleProtection(true, false);

      fileSecurity.SetOwner(user);

      // Attempt to create a unique file three times before giving up.
      // It is highly improbable that there will ever be a name clash,
      // therefore we do not check to see if the file first exists.
      for (int attempt = 0; attempt < 3; attempt++)
      {
        try
        {
          return new FileStream(Path.Combine(path, Path.GetRandomFileName()), FileMode.CreateNew,
                                FileSystemRights.FullControl, FileShare.None, bufferSize, options, fileSecurity);
        }
        catch (IOException)
        {
          if (attempt == 2)
            throw;
        }
      }
      // This code can never be reached.
      // The compiler thinks otherwise.
      throw new IOException();
    }
開發者ID:arangas,項目名稱:MediaPortal-1,代碼行數:42,代碼來源:PathUtility.cs

示例6: GetSecuritySettings

 static private FileSecurity GetSecuritySettings() {
    FileSecurity security = new FileSecurity();
    security.SetAccessRuleProtection(true, false);
    security.AddAccessRule(
       (FileSystemAccessRule) security.AccessRuleFactory(
                                 new NTAccount(
                                    WindowsIdentity.GetCurrent().Name),
                                 // Full control
                                 -1,
                                 false,
                                 InheritanceFlags.None,
                                 PropagationFlags.None,
                                 AccessControlType.Allow));
    return security;
 }
開發者ID:Nikolay-Krasan,項目名稱:DegreeWork,代碼行數:15,代碼來源:CredentialStore.cs

示例7: ProjectCanNotBeOpened

        public void ProjectCanNotBeOpened()
        {
            string projectFile = null;

            IdentityReference identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            FileSystemAccessRule rule = new FileSystemAccessRule(identity, FileSystemRights.Read, AccessControlType.Deny);

            FileSecurity security = null;

            try
            {
                // Does not have .sln or .vcproj extension so loads as project
                projectFile = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();

                security = new FileSecurity(projectFile, System.Security.AccessControl.AccessControlSections.All);
                security.AddAccessRule(rule);

                File.SetAccessControl(projectFile, security);

                ProjectRootElement p = ProjectRootElement.Open(projectFile);
            }
            catch (PrivilegeNotHeldException)
            {
                throw new InvalidProjectFileException("Running unelevated so skipping the scenario.");
            }
            finally
            {
                if (security != null)
                {
                    security.RemoveAccessRule(rule);
                }

                File.Delete(projectFile);
                Assert.AreEqual(false, File.Exists(projectFile));
            }
        }
開發者ID:ravpacheco,項目名稱:msbuild,代碼行數:36,代碼來源:ProjectRootElement_Tests.cs

示例8: SolutionCanNotBeOpened

        public void SolutionCanNotBeOpened()
        {
            string solutionFile = null;
            string tempFileSentinel = null;

            IdentityReference identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            FileSystemAccessRule rule = new FileSystemAccessRule(identity, FileSystemRights.Read, AccessControlType.Deny);

            FileSecurity security = null;

            try
            {
                tempFileSentinel = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
                solutionFile = Path.ChangeExtension(tempFileSentinel, ".sln");
                File.Copy(tempFileSentinel, solutionFile);

                security = new FileSecurity(solutionFile, System.Security.AccessControl.AccessControlSections.All);

                security.AddAccessRule(rule);

                File.SetAccessControl(solutionFile, security);

                ProjectRootElement p = ProjectRootElement.Open(solutionFile);
            }
            catch (PrivilegeNotHeldException)
            {
                throw new InvalidProjectFileException("Running unelevated so skipping this scenario.");
            }
            finally
            {
                if (security != null)
                {
                    security.RemoveAccessRule(rule);
                }

                File.Delete(solutionFile);
                File.Delete(tempFileSentinel);
                Assert.AreEqual(false, File.Exists(solutionFile));
            }
        }
開發者ID:ravpacheco,項目名稱:msbuild,代碼行數:40,代碼來源:ProjectRootElement_Tests.cs

示例9: CopyAccessControlList

        /// <summary>
        /// Copies the Access Control List (ACL) from one file to another and specify additional ACL rules on the destination file.
        /// </summary>
        /// <param name="pathToSourceFile">The path to the source file.</param>
        /// <param name="pathToDestinationFile">The path to the destination file.</param>
        /// <param name="additionalFileSystemAccessRules">An array of <see cref="FileSystemAccessRule"/>. The additional ACLs.</param>
        public static void CopyAccessControlList(string pathToSourceFile, string pathToDestinationFile, FileSystemAccessRule[] additionalFileSystemAccessRules)
        {
            if (additionalFileSystemAccessRules == null)
            {
                throw new ArgumentNullException("additionalFileSystemAccessRules");
            }

            CheckFilePathParameter("pathToSourceFile", pathToSourceFile);
            CheckFilePathParameter("pathToDestinationFile", pathToDestinationFile);
            FileSecurity sourceFileSecurity = File.GetAccessControl(pathToSourceFile);
            FileSecurity destinationFileSecurity = new FileSecurity();
            byte[] securityDescriptor = sourceFileSecurity.GetSecurityDescriptorBinaryForm();
            destinationFileSecurity.SetSecurityDescriptorBinaryForm(securityDescriptor);
            foreach (FileSystemAccessRule fileSystemAccessRule in additionalFileSystemAccessRules)
            {
                destinationFileSecurity.AddAccessRule(fileSystemAccessRule);
            }

            File.SetAccessControl(pathToDestinationFile, destinationFileSecurity);
        }
開發者ID:StealFocus,項目名稱:Core,代碼行數:26,代碼來源:FileSystem.cs

示例10: SetReadAccessOnDefinitionFile

		public static void SetReadAccessOnDefinitionFile(string definitionName, string user)
		{
			string filePathName = ScheduledJobStore.GetFilePathName(definitionName, "ScheduledJobDefinition");
			FileSecurity fileSecurity = new FileSecurity(filePathName, AccessControlSections.Access);
			FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(user, FileSystemRights.Read, AccessControlType.Allow);
			fileSecurity.AddAccessRule(fileSystemAccessRule);
			File.SetAccessControl(filePathName, fileSecurity);
		}
開發者ID:nickchal,項目名稱:pash,代碼行數:8,代碼來源:ScheduledJobStore.cs

示例11: TakeOwnership

        public static void TakeOwnership(string path)
        {
            try {
                using (var user = WindowsIdentity.GetCurrent())
                {
                    var ownerSecurity = new FileSecurity();
                    ownerSecurity.SetOwner(user.User);
                    File.SetAccessControl(path, ownerSecurity);

                    var accessSecurity = new FileSecurity();
                    accessSecurity.AddAccessRule(new FileSystemAccessRule(user.User, FileSystemRights.FullControl, AccessControlType.Allow));
                    File.SetAccessControl(path, accessSecurity);
                }
            } catch(UnauthorizedAccessException) {
                EndProcessAdmin();
            }
        }
開發者ID:Whitetigerswt,項目名稱:SAMP_AC_Extension,代碼行數:17,代碼來源:Misc.cs

示例12: SetReadAccessOnDefinitionFile

        /// <summary>
        /// Set read access on provided definition file for specified user.
        /// </summary>
        /// <param name="definitionName">Definition name</param>
        /// <param name="user">Account user name</param>
        public static void SetReadAccessOnDefinitionFile(
            string definitionName,
            string user)
        {
            string filePath = GetFilePathName(definitionName, DefinitionFileName);

            // Get file security for existing file.
            FileSecurity fileSecurity = new FileSecurity(
                filePath,
                AccessControlSections.Access);

            // Create rule.
            FileSystemAccessRule fileAccessRule = new FileSystemAccessRule(
                user,
                FileSystemRights.Read,
                AccessControlType.Allow);
            fileSecurity.AddAccessRule(fileAccessRule);

            // Apply rule.
            File.SetAccessControl(filePath, fileSecurity);
        }
開發者ID:dfinke,項目名稱:powershell,代碼行數:26,代碼來源:ScheduledJobStore.cs

示例13: RestrictAdminAccess

		private void RestrictAdminAccess(string path)
		{
			FileSecurity fileSecurity = new FileSecurity();
			fileSecurity.SetAccessRuleProtection(true, false);
			SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
			FileSystemRights fileSystemRight = FileSystemRights.FullControl;
			AccessControlType accessControlType = AccessControlType.Allow;
			FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(securityIdentifier, fileSystemRight, accessControlType);
			fileSecurity.AddAccessRule(fileSystemAccessRule);
			File.SetAccessControl(path, fileSecurity);
		}
開發者ID:nickchal,項目名稱:pash,代碼行數:11,代碼來源:PswaAuthorizationRuleManager.cs

示例14: setFileSecurity

        public void setFileSecurity(String path)
        {
            FileSecurity fs;
            FileSystemAccessRule fsRule = new FileSystemAccessRule(self.Name, FileSystemRights.FullControl, AccessControlType.Allow);
            foreach (String file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
            {
                try
                {
                    fs = new FileSecurity(file, AccessControlSections.Access);
                    fs.AddAccessRule(fsRule);
                    File.SetAccessControl(file, fs);
                    File.SetAttributes(file, FileAttributes.Normal);
                    fs.SetOwner(self.Owner);
                }
                catch (Exception err)
                {
                }

                FileInfo finfo = new FileInfo(file);
                finfo.Attributes = FileAttributes.Normal;
                FileSecurity sec = finfo.GetAccessControl();
                sec.SetAccessRule(fsRule);
                sec.SetOwner(self.Owner);

                Application.DoEvents();
                try
                {
                    File.Delete(file);
                    Application.DoEvents();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
                Application.DoEvents();
            }
        }
開發者ID:digitalhuman,項目名稱:SVNDelete,代碼行數:37,代碼來源:Form1.cs

示例15: AlphaFS___CreateWithFileSecurity

      public void AlphaFS___CreateWithFileSecurity()
      {
         Console.WriteLine("File.Create()");

         string pathExpected = Path.GetTempPath("AlphaFS CreateWithFileSecurityExpected");
         string pathActual = Path.GetTempPath("AlphaFS CreateWithFileSecurityActual");

         File.Delete(pathExpected);
         File.Delete(pathActual);

         FileSecurity expectedFileSecurity = new FileSecurity();
         expectedFileSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, AccessControlType.Allow));
         expectedFileSecurity.AddAuditRule(new FileSystemAuditRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.Read, AuditFlags.Success));

         using (new Alphaleonis.Win32.Security.PrivilegeEnabler(Alphaleonis.Win32.Security.Privilege.Security))
         {
            using (FileStream s1 = System.IO.File.Create(pathExpected, 4096, FileOptions.None, expectedFileSecurity))
            using (FileStream s2 = File.Create(pathActual, 4096, FileOptions.None, expectedFileSecurity))
            {
            }
         }

         string expectedFileSecuritySddl = System.IO.File.GetAccessControl(pathExpected).GetSecurityDescriptorSddlForm(AccessControlSections.All);
         string actualFileSecuritySddl = System.IO.File.GetAccessControl(pathActual).GetSecurityDescriptorSddlForm(AccessControlSections.All);

         Assert.AreEqual(expectedFileSecuritySddl, actualFileSecuritySddl);

         File.Delete(pathExpected, true);
         File.Delete(pathActual, true);
      }
開發者ID:Sicos1977,項目名稱:AlphaFS,代碼行數:30,代碼來源:FileTest.cs


注:本文中的System.Security.AccessControl.FileSecurity.AddAccessRule方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。