当前位置: 首页>>代码示例>>C#>>正文


C# FileSecurity.GetAccessRules方法代码示例

本文整理汇总了C#中System.Security.AccessControl.FileSecurity.GetAccessRules方法的典型用法代码示例。如果您正苦于以下问题:C# FileSecurity.GetAccessRules方法的具体用法?C# FileSecurity.GetAccessRules怎么用?C# FileSecurity.GetAccessRules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.AccessControl.FileSecurity的用法示例。


在下文中一共展示了FileSecurity.GetAccessRules方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RemoveAllExplicitAccessRules

        /// <summary>
        /// Removes all explicit access rules from the supplied file.
        /// </summary>
        /// <param name="path">The path to the file to have access removed on.</param>
        /// <param name="security">The FileSecurity object of the file once changed.</param>
        /// <param name="commitChanges">Indicates whether changes should be commited to this file. Useful when combining multiple commands.</param>
        /// <returns>True if access was removed. False otherwise.</returns>
        public static bool RemoveAllExplicitAccessRules(string path, out FileSecurity security, bool commitChanges)
        {
            // Check that a path was supplied.
            if (!string.IsNullOrEmpty(path))
            {
                // The path was supplied.

                // Check whether the file exists.
                if (SystemFile.Exists(path))
                {
                    // The file exists.

                    // Remove existing explicit permissions.
                    security = GetSecurityObject(path);
                    if (security != null)
                    {
                        AuthorizationRuleCollection rules = security.GetAccessRules(true, false, typeof(System.Security.Principal.SecurityIdentifier));
                        foreach (AuthorizationRule rule in rules)
                        {
                            security.RemoveAccessRule((FileSystemAccessRule)rule);
                        }
                        // Commit the changes if necessary.
                        if (commitChanges)
                        {
                            try
                            {
                                SystemFile.SetAccessControl(path, security);
                            }
                            catch (IOException)
                            {
                                // An I/O error occurred while opening the file.
                                return false;
                            }
                            catch (UnauthorizedAccessException)
                            {
                                // The path parameter specified a file that is read-only.
                                // The operation is not supported on the current platform.
                                // Or the current process does not have the required permission.
                                return false;
                            }
                        }
                        return true;
                    }
                    else
                    {
                        // Unable to get the file's security object.
                        return false;
                    }
                }
                else
                {
                    // The file does not exist.
                    security = null;
                    return false;
                }
            }
            else
            {
                // A path was not supplied.
                security = null;
                return false;
            }
        }
开发者ID:TheHunter,项目名称:Galactic,代码行数:70,代码来源:File.cs

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


注:本文中的System.Security.AccessControl.FileSecurity.GetAccessRules方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。