.NET 4.5.2下替换方法文件依赖为文本框内容报PathTooLongException如何解决
解决方案
方案1:启用系统长路径支持+调用Win32原生API
- 先修改系统注册表开启长路径支持:按下Win+R输入
regedit打开注册表,定位到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem,找到LongPathsEnabled项,类型为DWORD,值设为1,不存在则手动新建该项。 - 在项目的app.config或程序清单文件中添加配置,声明应用支持长路径:
<application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware> </windowsSettings> </application>
- 若System.IO默认方法仍拦截长路径,可直接调用Win32原生API读取文件,示例代码如下:
using System.Runtime.InteropServices; using System.Text; public class LongPathFileReader { [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr CreateFileW( string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool ReadFile( IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool CloseHandle(IntPtr hObject); private const uint GENERIC_READ = 0x80000000; private const uint OPEN_EXISTING = 3; private const uint FILE_SHARE_READ = 0x00000001; public static string ReadAllText(string path) { // 给路径加\\?\前缀兼容长路径 string longPath = @"\\?\" + Path.GetFullPath(path); IntPtr hFile = CreateFileW(longPath, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (hFile == IntPtr.Zero || hFile == (IntPtr)(-1)) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } try { uint fileSizeHigh = 0; uint fileSizeLow = GetFileSize(hFile, out fileSizeHigh); long fileSize = ((long)fileSizeHigh << 32) + fileSizeLow; byte[] buffer = new byte[fileSize]; uint bytesRead; ReadFile(hFile, buffer, (uint)fileSize, out bytesRead, IntPtr.Zero); // 可根据实际文件编码调整Encoding参数 return Encoding.UTF8.GetString(buffer, 0, (int)bytesRead); } finally { CloseHandle(hFile); } } [DllImport("kernel32.dll")] private static extern uint GetFileSize(IntPtr hFile, out uint lpFileSizeHigh); }
将原有代码中的File.ReadAllText(projectPath)替换为LongPathFileReader.ReadAllText(projectPath)即可。
方案2:使用8.3格式短路径别名
如果不方便修改系统配置,可调用Win32 API获取文件对应的短路径,再用短路径调用System.IO的原生方法,示例代码:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer); public static string GetShortPath(string longPath) { StringBuilder shortPath = new StringBuilder(256); GetShortPathName(longPath, shortPath, (uint)shortPath.Capacity); return shortPath.ToString(); }
读取文件前先把原始路径传入该方法得到短路径,再用短路径调用File.ReadAllText就不会触发长度限制。
注意事项
- 现有代码存在逻辑问题:
Button_Click方法中调用ExecuteProject()没有传参,且ExecuteProject内部直接new View()获取文本框值是无效的,新实例的文本框默认是空值,应该直接获取当前已打开的窗口的文本框内容,或者将文本内容作为参数传入ExecuteProject方法。 - 请先确认文本框中存储的是正确的文件全路径,而非文件内容,误将文件内容作为路径传入也会触发路径超长错误。
内容的提问来源于stack exchange,提问作者Tymaca
相关产品推荐
相关产品推荐

