간단한 솔루션
은
가
가
까지 약 32,767 문자의 파일 경로를 허용 할 것이다 이는 UNC 가능 경로를 사용하는 것입니다
string longPathEnabledFileName = Path.ToLongPath("C:\SomeVeryLongPath\....");
FileStream fs = new FileStream(longPathEnabledFileName);
그러면 프레임 워크에 260 문자의 MAX_PATH 제한을 우회하도록 지시하는 \\? \가있는 경로 앞에 추가됩니다. 불행하게도의 접두사 \\? \입니다 하지
이는 WINAPI 솔루션으로 우리를 잎 (버전 4.0로) 작성시 닷넷에서 지원 SafeFileHandle을 사용하려면 Kernel32.dll을 참조하십시오.
// This code snippet is provided under the Microsoft Permissive License.
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern SafeFileHandle CreateFile(
string lpFileName,
EFileAccess dwDesiredAccess,
EFileShare dwShareMode,
IntPtr lpSecurityAttributes,
ECreationDisposition dwCreationDisposition,
EFileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
public static void TestCreateAndWrite(string fileName) {
string formattedName = @"\\?\" + fileName;
// Create a file with generic write access
SafeFileHandle fileHandle = CreateFile(formattedName,
EFileAccess.GenericWrite, EFileShare.None, IntPtr.Zero,
ECreationDisposition.CreateAlways, 0, IntPtr.Zero);
// Check for errors
int lastWin32Error = Marshal.GetLastWin32Error();
if (fileHandle.IsInvalid) {
throw new System.ComponentModel.Win32Exception(lastWin32Error);
}
// Pass the file handle to FileStream. FileStream will close the
// handle
using (FileStream fs = new FileStream(fileHandle,
FileAccess.Write)) {
fs.WriteByte(80);
fs.WriteByte(81);
fs.WriteByte(83);
fs.WriteByte(84);
}
}
,
구글 코드에서이 모든 작업을 통해 캡슐화하는 라이브러리라는 제타 긴 경로
어떻게 보 였는지 궁금합니다. 내가 Windows API를 변경, 아니면 그냥 놓친거야? –
Hmmm, 그렇습니다. Path와 같은 것은 존재하지 않습니다 .ToLongPath는 BCL로부터의 미래에 대한 제안 일뿐입니다. 불행히도 .net 프레임 워크는 \\? \의 간단한 솔루션을 지원하지 않으므로 http://blogs.msdn.com/b/bclteam/archive/에 설명 된 WinApi PInvoke 함수를 사용해야합니다. 2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton.aspx 또는 제타 긴 경로 사용 - 의견을 반영하여이 – Anastasiosyal
을 반영하여 게시물을 편집하십시오. 실제로 제타를보고 있었지만 그 dll을 사용하여 디렉토리를 만들 수는 없습니다. 그 곳에서 찾을 수는 없지만 파일 컨트랙션을 얻을 수 있다고 말하면서이 컨텍스트에서 필요없는 건 두려워합니다. –