Question

I am using below code to check Permission on Directories in Windows.

I am using in Unity 3.5.7f6 with "PC & Mac" Platforms in BuildSettings.

I am getting following exception:

  1. PlatformNotSupportedException
  2. UnauthorizedAccessException

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

I have tried printing OS version:

Environment.OSVersion.Platform

it prints: WindowsNT 5.1.2600 (which is XP)

All i am trying above, is to get whether a folder has Permission (Read OR write).

Help me to resolve this.

Was it helpful?

Solution

Short answer: At the moment this feature is simply not implemented by Unity.

Unity is using a pretty old version of Mono. There seems to be some licensing issues regarding mobile platforms and consoles, so that they are not able to simple use the newest version of Mono.

As you can see in the Mono source code for Unity 4.3, the constructor for DirectorySecurity simply raises an PlatformNotSupportedException. And because GetAccessControl depends on DirectorySecurity it will also just raise an exception.

If you explain why you need the permissions or what your ultimate goal is, we may be able to find another solution for your problem.

OTHER TIPS

It appears that some of the directory security code is not implemented in a platform independent way in Mono, even in 4.8.

I came here with a similar problem when trying to edit a folder's permissions when running Mono on a Unix box. That raised a PlatformNotSupportedException from the NativeObjectSecurity class Hopefully this helps anyone else in a similar boat...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top