在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);
            
        }
登录后复制


08-16 03:46