简体   繁体   English

获取超过 260 个字符的过长文件路径的安全信息:C#

[英]Get Security Information of a too long file path above 260 characters: C#

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.不幸的是,一些文件和目录路径分别超过了它们的字符限制 260/248。 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.我发现了很多关于使用 Win32 P/Invoke、使用 .NET Framework 4.6.2 等的信息。我能够使用Kim Hamilton 的代码来遍历路径超过长度限制的每个文件和目录,但我无法使用它来获取安全信息。

Below is my simple C# code containing a path which is above 260 characters.下面是我的简单 C# 代码,其中包含一个超过 260 个字符的路径。 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);

        }
    }
}

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).抛出的异常如前所述:无效名称,无效参数(在我的情况下发生在SetAccessControl上)。 .NET 4.7.2 .NET 4.7.2

What helped is the special syntax: \\\\?\\ for local paths or \\\\?\\UNC\\ for network shares.特殊语法有什么帮助: \\\\?\\用于本地路径或\\\\?\\UNC\\用于网络共享。

So for the example in question (server share) it would be something like below:因此,对于有问题的示例(服务器共享),它将类似于以下内容:

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

I saw in some other posts that installing .NET Framework 4.6.2 does help.我在其他一些帖子中看到安装 .NET Framework 4.6.2 确实有帮助。 As the last resort, I tried it.作为最后的手段,我尝试了它。 I had Visual Studio 2015. I installed .NET Framework 4.6.2.我有 Visual Studio 2015。我安装了 .NET Framework 4.6.2。 It still did not work.它仍然没有工作。 Then I installed Visual Studio 2017 and chose .NET Framework 4.6.2 .然后我安装了Visual Studio 2017并选择了.NET Framework 4.6.2 It did eliminate Path Too Long Exception.它确实消除了路径太长异常。 But it gave a new exception called Invalid name, Invalid parameter.但它给出了一个名为 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.它的原始值是 0。我将它设置为 1。路径太长和无效名称、无效参数异常都消失了。 I believe this registry key does not exist in computer that does not have .NET Framework 4.6.2.我相信没有 .NET Framework 4.6.2 的计算机中不存在此注册表项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM