0

I am trying to get security information of some files and directories inside a network folder. Unfortunately some files and directories path exceed their character limits 260/248 respectively. I found so many information to use Win32 P/Invoke, use .NET Framework 4.6.2 etc. I was able to use a code by Kim Hamilton to iterate through each files and directories inside whose path exceeds the length limit but I could not use it to get the security information.

Below is my simple C# code containing a path which is above 260 characters. It will throw a Path Too Long Exception. Could you please help me solve it in this scenario.

using System.IO;
using System.Security.AccessControl;

namespace Microsoft.Experimental.IO
{
    class Program
    {
        public static void Main(string[] args)
        {
            string path = @"\\Domain\UserData\VeryLongPath";  //This is above 260 characters
            DirectoryInfo info = new DirectoryInfo(path);
            DirectorySecurity security = Directory.GetAccessControl(path);

        }
    }
}
Merin Nakarmi
  • 3,148
  • 3
  • 35
  • 42
  • 1
    The error you have having is due to DOS limits. The best solution is to mount a drive on the PC and then use the drive letter to access the file info. – jdweng Apr 05 '18 at 00:00
  • @jdweng: That sounds a nice idea. It will save some characters indeed. But what if it will still exceed the limit of 260 characters? – Merin Nakarmi Apr 05 '18 at 00:13
  • 1
    Then map the drive to a different folder closer to the actual folder. There isn't a limit on the map drive path. Used this method plenty of times. Often have run into this issue with Excel Files. So if you map drive \\Domain\UserData\VeryLongPath to P: then you file name is only P:\\filename – jdweng Apr 05 '18 at 06:32
  • @jdweng: Sadly this did not work for me friend. I have P drive and G drive mapped. It gave same exceptions on both drives. :( I appreciate your effort trying to help me. – Merin Nakarmi Apr 06 '18 at 00:28
  • Then you are not using the mapped drive to shorten your path name. – jdweng Apr 06 '18 at 06:54

2 Answers2

1

Accepted answer didn't really work for me although I checked the registry key (it seems to be enabled by default nowadays). The exception thrown was as mentioned: invalid name, invalid parameter (happened on SetAccessControl in my case). .NET 4.7.2

What helped is the special syntax: \\?\ for local paths or \\?\UNC\ for network shares.

So for the example in question (server share) it would be something like below:

var security = Directory.GetAccessControl($@"\\?\UNC\{path.TrimStart('\\')}");
Programmierus
  • 167
  • 11
0

I saw in some other posts that installing .NET Framework 4.6.2 does help. As the last resort, I tried it. I had Visual Studio 2015. I installed .NET Framework 4.6.2. It still did not work. Then I installed Visual Studio 2017 and chose .NET Framework 4.6.2. It did eliminate Path Too Long Exception. But it gave a new exception called Invalid name, Invalid parameter.

One of my colleague suggested me to check the value of registry

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled

It's original value was 0. I set it to 1. And both the Path Too Long and Invalid Name, Invalid parameter exception were gone. I believe this registry key does not exist in computer that does not have .NET Framework 4.6.2.

Merin Nakarmi
  • 3,148
  • 3
  • 35
  • 42