首页 >数据库 >mysql教程 >Assign directory/file access rights to 'everyone&

Assign directory/file access rights to 'everyone&

WBOY
WBOY原创
2016-06-07 15:37:021265浏览

在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);
            
        }


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn