C#设置文件(夹)权限

C#设置账户对文件的访问权限为完全访问

        /// <summary>
        /// 将账户对文件的访问权限设置为完全访问
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="userName">账户名</param>
        private void SetFilePermission(string filePath,string userName)
        {
            FileInfo fi = new FileInfo(filePath);
            FileSecurity fileSecurity = fi.GetAccessControl();
            fileSecurity.AddAccessRule
                (new FileSystemAccessRule
                    (userName, FileSystemRights.FullControl,
                    AccessControlType.Allow));
            fi.SetAccessControl(fileSecurity);
        }

C#设置账户对文件的访问权限为完全访问

        /// <summary>
        /// 将账户对文件夹的访问权限设置为完全访问
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <param name="userName">账户名</param>
        private void SetFolderPermission(string folderPath,string userName)
        {
            DirectoryInfo diInfo = 
                new DirectoryInfo(folderPath);
            DirectorySecurity dirSecurity = diInfo.GetAccessControl();
            dirSecurity.AddAccessRule
                (new FileSystemAccessRule
                    (userName, FileSystemRights.FullControl, 
                    AccessControlType.Allow));
            diInfo.SetAccessControl(dirSecurity);
        }

posted @ 2015-02-03 10:48  <码仔>  阅读(865)  评论(0编辑  收藏  举报