The undocumented approach suggested by @RbMm in the comments (setting the PEB
's IsLongPathAwareProcess
bit to 1) worked (at least on the version of Windows 10 I was on: 1903). Since that approach is a native C/C++ approach, I just needed to translate that into something consumable by C#.
There are multiple approaches that can be used (C++/CLI, pinvoke). Since I was just doing "proof of concept", I decided to just use pinvoke and be a bit lazy in the quality of the code.
Notes:
- This code does no version checking to see if it is running on a version of Windows 10 that supports
IsLongPathAwareProcess
.
- Limited error checking
- I only included the beginning part of the
PEB
structure, since that is all that is necessary to access the bit I want to modify.
- There's very likely a more concise way to set the
IsLongPathAwareProcess
bit than the approach I used (Marshal.StructureToPtr
)
- Includes a test that works when the call to
SetIsLongPathAwareProcess
is executed and fails when it is not called
- Notice in the test code that I delete
C:\Test
(so that I can re-run the test multiple times without the directory already existing)
Test Code:
class Program
{
static void Main(string[] args)
{
SetIsLongPathAwareProcess();
string reallyLongDirectory = @"C:\Test\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
reallyLongDirectory = reallyLongDirectory + @"\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
reallyLongDirectory = reallyLongDirectory + @"\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
CreateDirTest(reallyLongDirectory);
Directory.Delete(@"C:\Test", recursive: true);
}
private static void CreateDirTest(string name)
{
Console.WriteLine($"Creating a directory that is {name.Length} characters long");
// Test managed CreateDirectory
Directory.CreateDirectory(name);
// Test Native CreateDirectory. Note this method will only create the "last part" of the directory (not the full path).
// Also note that it fails if the directory already exists.
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createdirectorya
string nativeName = Path.Combine(name, "TestEnding");
var r = Native.CreateDirectory(nativeName, null);
if (!r)
{
int currentError = Marshal.GetLastWin32Error();
Debug.Fail($"Native.CreateDirectory failed: {currentError}");
}
}
// Adapted from https://www.pinvoke.net/default.aspx/ntdll.ntqueryinformationprocess
private static void SetIsLongPathAwareProcess()
{
var currentProcess = Process.GetCurrentProcess();
IntPtr hProc = currentProcess.Handle;
IntPtr pPbi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Native.PROCESS_BASIC_INFORMATION)));
IntPtr outLong = Marshal.AllocHGlobal(sizeof(long));
int status = Native.NtQueryInformationProcess(hProc, 0, pPbi, (uint)Marshal.SizeOf(typeof(Native.PROCESS_BASIC_INFORMATION)), outLong);
Marshal.FreeHGlobal(outLong);
//STATUS_SUCCESS = 0
if (status == 0)
{
var pbi = Marshal.PtrToStructure<Native.PROCESS_BASIC_INFORMATION>(pPbi);
var pPeb = pbi.PebBaseAddress;
var peb = Marshal.PtrToStructure<Native.PEB_Beginning>(pPeb);
var bitField1 = peb.BitField1;
peb.BitField1 = bitField1 | Native.PEBBitField1.IsLongPathAwareProcess;
Marshal.StructureToPtr<Native.PEB_Beginning>(peb, pPeb, false);
}
//Free allocated space
Marshal.FreeHGlobal(pPbi);
}
static class Native
{
// http://pinvoke.net/default.aspx/Structures/PROCESS_BASIC_INFORMATION.html
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct PROCESS_BASIC_INFORMATION
{
public IntPtr ExitStatus;
public IntPtr PebBaseAddress;
public IntPtr AffinityMask;
public IntPtr BasePriority;
public UIntPtr UniqueProcessId;
public IntPtr InheritedFromUniqueProcessId;
public int Size
{
get { return (int)Marshal.SizeOf(typeof(PROCESS_BASIC_INFORMATION)); }
}
}
[Flags]
internal enum PEBBitField1 : byte
{
None = 0,
ImageUsesLargePages = 1 << 0,
IsProtectedProcess = 1 << 1,
IsImageDynamicallyRelocated = 1 << 2,
SkipPatchingUser32Forwarders = 1 << 3,
IsPackagedProcess = 1 << 4,
IsAppContainer = 1 << 5,
IsProtectedProcessLight = 1 << 6,
IsLongPathAwareProcess = 1 << 7
}
// Note: this only contains the "beginning" of the PEB structure
// but that is all we need for access to the IsLongPathAwareProcess bit
// Used as a guide: https://github.com/processhacker/processhacker/blob/master/phnt/include/ntpebteb.h#L75
// Related: https://learn.microsoft.com/en-us/windows/win32/api/winternl/ns-winternl-peb
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct PEB_Beginning
{
Byte InheritedAddressSpace;
Byte ReadImageFileExecOptions;
Byte BeingDebugged;
public PEBBitField1 BitField1;
};
// https://www.pinvoke.net/default.aspx/ntdll.ntqueryinformationprocess
[DllImport("ntdll.dll", SetLastError = true)]
internal static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, IntPtr processInformation, uint processInformationLength, IntPtr returnLength);
// The below is for test purposes only:
// Include CreateDirectory for testing if the long path aware changes work or not.
// Requires SECURITY_ATTRIBUTES
// http://pinvoke.net/default.aspx/kernel32/CreateDirectory.html?diff=y
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)]
internal static extern bool CreateDirectory(String path, SECURITY_ATTRIBUTES lpSecurityAttributes);
// https://www.pinvoke.net/default.aspx/Structures/SECURITY_ATTRIBUTES.html
[StructLayout(LayoutKind.Sequential)]
internal class SECURITY_ATTRIBUTES
{
internal int nLength = 0;
// don't remove null, or this field will disappear in bcl.small
internal unsafe byte* pSecurityDescriptor = null;
internal int bInheritHandle = 0;
}
}
}
If you use this code in a Console App that is naturally longPathAware
, you can still test that it works correctly, by manually setting longPathAware
to false
in the app.manifest
:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">false</longPathAware>
</windowsSettings>
</application>