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


C# FileSecurity.SetOwner方法代碼示例

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


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

示例1: GetOwnershipTest2

        public void GetOwnershipTest2()
        {
            // Arrange
            var tmpDir = Path.Combine(Path.GetTempPath(), "dirtools-test-" + Guid.NewGuid().ToString());
            Directory.CreateDirectory(tmpDir);

            var tmpFile = Path.Combine(tmpDir, "asdf");

            var localSystem = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);

            var fileSec = new FileSecurity();
            fileSec.SetOwner(localSystem);

            File.Create(tmpFile, 1, FileOptions.None, fileSec).Dispose();

            // Act
            var curIdentity = new NTAccount(Environment.UserDomainName, Environment.UserName);
            DirectoryTools.GetOwnershipForDirectory(tmpFile, curIdentity);

            // Assert
            var curFilesec = new FileSecurity(tmpFile, AccessControlSections.Owner);
            IdentityReference owner = curFilesec.GetOwner(typeof(NTAccount));
            Assert.IsTrue(curIdentity == owner);
        }
開發者ID:solakian,項目名稱:windows-isolation,代碼行數:24,代碼來源:TestDirectoryTools.cs

示例2: RemoverPermissaoProcessos

        private static void RemoverPermissaoProcessos()
        {
            var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            Console.Write("Removendo permissões Gbp... ");
            foreach (var dir in CaminhosGBP)
            {
                try
                {
                    var dirInfo = new DirectoryInfo(Environment.ExpandEnvironmentVariables(dir));
                    if (dirInfo.Exists)
                    {

                        foreach (var f in dirInfo.GetFiles())
                        {
                            if (Path.GetExtension(f.Name).ToLower() == ".exe")
                            {
                                var fs = new FileSecurity();
                                fs.SetOwner(new NTAccount(WindowsIdentity.GetCurrent().Name));

                                fs.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.ExecuteFile, AccessControlType.Deny));
                                f.SetAccessControl(fs);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                }
            }
            Console.WriteLine("OK.");
        }
開發者ID:junalmeida,項目名稱:gbpkiller,代碼行數:32,代碼來源:Program.cs

示例3: 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

示例4: GrantAccess

        private static void GrantAccess(string filepath)
        {
            FileSecurity fs;

            try
            {
                fs = File.GetAccessControl(filepath);
            }
            catch
            {
                fs = new FileSecurity();
            }

            //var sid = fs.GetOwner(typeof(SecurityIdentifier));
            var ntAccount = new NTAccount(Environment.UserDomainName, Environment.UserName);
            try
            {
                fs.SetOwner(ntAccount);
                File.SetAccessControl(filepath, fs);

                var currentRules = fs.GetAccessRules(true, false, typeof(NTAccount));
                var newRule = new FileSystemAccessRule(
                    ntAccount, FileSystemRights.FullControl,
                    AccessControlType.Allow);
                fs.AddAccessRule(newRule);
                File.SetAccessControl(filepath, fs);

                File.SetAttributes(filepath, FileAttributes.Normal);
            }
            catch { }
            finally { fs = null; ntAccount = null; }
        }
開發者ID:targitaj,項目名稱:m3utonetpaleyerxml,代碼行數:32,代碼來源:MainWindow.xaml.cs

示例5: 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

示例6: 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


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