vista permissions
-
heiho
wie kann ich unter vista pruefen ob man zugroffsrechte auf bestimmte verzeichnisse hat?
so:bool HasPermissions(string path) { try { Directory.GetDirectories(path); } catch { return false; } return true; }
ist das verdammt haesslich, und das moechte ich nicht implementieren /=
-
dieses hier ist anscheinend nur fuer Vista, geht aber nicht unter XP
class Permissions { public static bool HasPermissions(string path) { if(GetDirectoryPermissions(Environment.UserName, Environment.UserDomainName, path) == 0) return false; return true; } private static FileSystemRights GetDirectoryPermissions(string user, string domainName, string folderPath) { if(!Directory.Exists(folderPath)) return 0; string identityReference = ((domainName + @"\" + user) as string).ToLower(); DirectorySecurity dirSecurity = Directory.GetAccessControl(folderPath, AccessControlSections.Access); foreach(FileSystemAccessRule fsRule in dirSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) { if(fsRule.IdentityReference.Value.ToLower() == identityReference) return (fsRule.FileSystemRights); } return 0; } }
-
habs jetzt erstma so gemacht das ich das nur abfrage wenn es Vista ist, amsonsten geh ich von true aus
enum WindowsVersion { Unknown, Windows98, WindowsME, WindowsNT, Windows2000, WindowsXP, Windows2003, WindowsVista } class Permissions { private static bool isInit = false; private static WindowsVersion _version = new WindowsVersion(); public static bool HasPermissions(string path) { if(!isInit) _version = GetWindowsVersion(); switch(_version) { case WindowsVersion.WindowsVista: if(GetDirectoryPermissionsVista(Environment.UserName, Environment.UserDomainName, path) == 0) return false; return true; default: return true; } } private static FileSystemRights GetDirectoryPermissionsVista(string user, string domainName, string folderPath) { if(!Directory.Exists(folderPath)) return 0; string identityReference = ((domainName + @"\" + user) as string).ToLower(); DirectorySecurity dirSecurity = Directory.GetAccessControl(folderPath, AccessControlSections.Access); foreach(FileSystemAccessRule fsRule in dirSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) { if(fsRule.IdentityReference.Value.ToLower() == identityReference) return (fsRule.FileSystemRights); } return 0; } private static WindowsVersion GetWindowsVersion() { isInit = true; OperatingSystem osInfo = System.Environment.OSVersion; if(osInfo.Platform == PlatformID.Win32Windows) { if(osInfo.Version.Minor == 10) return WindowsVersion.Windows98; if(osInfo.Version.Minor == 90) return WindowsVersion.WindowsME; } if(osInfo.Platform == PlatformID.Win32NT) { if(osInfo.Version.Major == 4) return WindowsVersion.WindowsNT; if(osInfo.Version.Major == 5) { switch(osInfo.Version.Minor) { case 0: return WindowsVersion.Windows2000; case 1: return WindowsVersion.WindowsXP; case 2: return WindowsVersion.WindowsVista; } } if(osInfo.Version.Major == 6) if(osInfo.Version.Minor == 0) return WindowsVersion.WindowsVista; } return WindowsVersion.Unknown; } }