我正在使用以下代码检查 Windows 中目录的权限。
我在 Unity 3.5.7f6 中使用 BuildSettings 中的“PC & Mac”平台。
我遇到以下异常:
- PlatformNotSupportedException
未授权访问异常
string l_userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; DirectorySecurity folderSecurity; //Way 1: Getting **PlatformNotSupportedException** with below line. folderSecurity = new DirectorySecurity(a_directoryPath, AccessControlSections.Audit); //Way 1---ENDS HERE //Way 2: Getting **UnauthorizedAccessException** in last line of Way 2 block. DirectoryInfo l_directory = new DirectoryInfo(a_directoryPath); folderSecurity = l_directory.GetAccessControl(AccessControlSections.Audit); //Way 2---ENDS HERE foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) { string l_domain_userName = fileSystemAccessRule.IdentityReference.Value; if (l_domain_userName.Equals(l_userName)) { string l_userRights = fileSystemAccessRule.FileSystemRights.ToString(); Debug.Log(l_domain_userName + ":" + l_userRights); if (fileSystemAccessRule.FileSystemRights.HasFlag(a_rightToCheck)) { Debug.Log("-----have Accesss:" + a_rightToCheck); l_isGranted = true; break; } } }
我试过打印操作系统版本:
Environment.OSVersion.Platform
它打印: WindowsNT 5.1.2600(即 XP)
我在上面尝试的是获取文件夹是否具有权限(读取或写入)。
帮我解决这个问题。
最佳答案
简短回答:目前 Unity 根本没有实现此功能。
Unity 使用的是相当旧的 Mono 版本。移动平台和控制台似乎存在一些许可问题,因此他们无法简单地使用最新版本的 Mono。
正如您在 Mono source code 中看到的那样对于 Unity 4.3,DirectorySecurity
的构造函数只是引发一个 PlatformNotSupportedException
。因为 GetAccessControl
依赖于 DirectorySecurity
它也将只是 raise an exception .
如果您解释为什么需要这些权限或您的最终目标是什么,我们也许能够为您的问题找到其他解决方案。
关于c# - Unity-Mono 在检查目录权限时抛出 PlatformNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23429615/