Home > Database > Mysql Tutorial > body text

Assign directory/file access rights to 'everyone&

WBOY
Release: 2016-06-07 15:37:02
Original
1236 people have browsed it

在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;
Copy after login


两个函数

 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;
        }
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!