Home  >  Article  >  Database  >  Assign directory/file access rights to 'everyone&

Assign directory/file access rights to 'everyone&

WBOY
WBOYOriginal
2016-06-07 15:37:021237browse

在win7 UAC下如果user1创建了一个文件,那么当user2登陆后默认对这个文件是无权编辑的。 解决方案就是user1在创建的时候把这个文件的权限给everyone;或者关掉UAC,把制定文件夹或者文件的权利给everyone 代码如下 引用 using System.Security.AccessControl;

在win7 UAC下如果user1创建了一个文件,那么当user2登陆后默认对这个文件是无权编辑的。


解决方案就是user1在创建的时候把这个文件的权限给everyone;或者关掉UAC,把制定文件夹或者文件的权利给everyone


代码如下

引用

using System.Security.AccessControl;
using System.Security.Principal;
using System.Security;


两个函数

 public static bool SetDirectoryAccessControl(string path)
        {
            try
            {
                DirectorySecurity sec = Directory.GetAccessControl(path);
                SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
                Directory.SetAccessControl(path, sec);
                return true;
            }
            catch (Exception exp)
            {
                // loggings
                MessageBox.Show("error in set directory access: " + exp.Message);
            }
            return false;
        }
        public static bool SetFileAccessControl(string filename)
        {
            try
            {
                FileSecurity sec = File.GetAccessControl(filename);
                SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.None, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
                File.SetAccessControl(filename, sec);
                return true;
            }
            catch (Exception exp)
            {
                // loggings
                MessageBox.Show("error in setting file access: " + exp.Message );
            }
            return false;
        }

如何判断用户是否是管理员
public static bool IsAdministrator()
        {
            var identity = WindowsIdentity.GetCurrent( );
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
            
        }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn