win 10 C#解决访问长路径失败,并指定程序具有admin权限

Posted superfeeling

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了win 10 C#解决访问长路径失败,并指定程序具有admin权限相关的知识,希望对你有一定的参考价值。

https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN
https://docs.microsoft.com/en-us/archive/blogs/jeremykuhne/net-4-6-2-and-long-paths-on-windows-10
.NetFramework 选择4.6.2
右键项目,添加manifest

这个文件中下图注释部分取消注释,并添加

这个文件中下图注释部分取消注释,并添加

 <application xmlns="urn:schemas-microsoft-com:asm.v3">
  <windowsSettings>
    <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
  </windowsSettings>
</application>

修改注册表
HKLM\\SYSTEM\\CurrentControlSet\\Control\\FileSystem LongPathsEnabled (Type: REG_DWORD) 设置为1

修改policy all setting 中enable win32 long paths

设置程序具有admin权限

<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
             If you want to change the Windows User Account Control level replace the 
             requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel element will disable file and registry virtualization. 
            Remove this element if your application requires this virtualization for backwards
            compatibility.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>

测试代码

string reallyLongDirectory = @"C:\\Build\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    reallyLongDirectory = reallyLongDirectory + @"\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    reallyLongDirectory = reallyLongDirectory + @"\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

                    //Console.WriteLine($"Creating a directory that is {rea

转:https://blog.csdn.net/weixin_43632687/article/details/104750738

以上是关于win 10 C#解决访问长路径失败,并指定程序具有admin权限的主要内容,如果未能解决你的问题,请参考以下文章

Win10VPN连接失败未进行远程连接,因为远程访问服务器的名称问题没有解决

与长路径一起使用时,CreateDirectory 失败并出现错误 123

怎样解决windows无法访问指定设备路径或文件?

windows无法访问指定设备路径或文件怎么解决

windows7管理员文件无法运行 无法访问指定设备 路径或文件

Windows无法访问指定设备,路径或文件怎么办