热门标签 | HotTags
当前位置:  开发笔记 > 开发工具 > 正文

Assigndirectory/fileaccessrightsto'everyone&

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

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

推荐阅读
author-avatar
船长2502860123
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有