Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1ea580b

Browse files
authored
Define Interop.Kernel32.MAX_PATH (#15952)
* Define Interop.Kernel32.MAX_PATH For consistency with CoreFX and coding conventions.
1 parent 7f4fbdb commit 1ea580b

File tree

8 files changed

+30
-24
lines changed

8 files changed

+30
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
internal partial class Interop
6+
{
7+
internal partial class Kernel32
8+
{
9+
internal const int MAX_PATH = 260;
10+
}
11+
}

src/mscorlib/shared/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@
643643
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetTempPathW.cs" />
644644
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.Globalization.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
645645
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.LockFile.cs" />
646+
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.MAX_PATH.cs" />
646647
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.OutputDebugString.cs" />
647648
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs" />
648649
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_NativeOverlapped.cs" />

src/mscorlib/shared/System/IO/Path.Windows.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public static partial class Path
2727
(char)31
2828
};
2929

30-
// The max total path is 260, and the max individual component length is 255.
31-
// For example, D:\<256 char file name> isn't legal, even though it's under 260 chars.
32-
internal const int MaxPath = 260;
33-
3430
// Expands the given path to a fully qualified path.
3531
public static string GetFullPath(string path)
3632
{
@@ -93,8 +89,8 @@ public static string GetFullPath(string path)
9389

9490
public static string GetTempPath()
9591
{
96-
StringBuilder sb = StringBuilderCache.Acquire(MaxPath);
97-
uint r = Interop.Kernel32.GetTempPathW(MaxPath, sb);
92+
StringBuilder sb = StringBuilderCache.Acquire(Interop.Kernel32.MAX_PATH);
93+
uint r = Interop.Kernel32.GetTempPathW(Interop.Kernel32.MAX_PATH, sb);
9894
if (r == 0)
9995
throw Win32Marshal.GetExceptionForLastWin32Error();
10096
return GetFullPath(StringBuilderCache.GetStringAndRelease(sb));
@@ -106,7 +102,7 @@ public static string GetTempFileName()
106102
{
107103
string path = GetTempPath();
108104

109-
StringBuilder sb = StringBuilderCache.Acquire(MaxPath);
105+
StringBuilder sb = StringBuilderCache.Acquire(Interop.Kernel32.MAX_PATH);
110106
uint r = Interop.Kernel32.GetTempFileNameW(path, "tmp", 0, sb);
111107
if (r == 0)
112108
throw Win32Marshal.GetExceptionForLastWin32Error();

src/mscorlib/shared/System/ReadOnlySpan.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ public bool IsEmpty
165165
/// <exception cref="System.IndexOutOfRangeException">
166166
/// Thrown when index less than 0 or index greater than or equal to Length
167167
/// </exception>
168-
169168
public ref readonly T this[int index]
170169
{
171170
#if PROJECTN

src/mscorlib/src/System/Threading/EventWaitHandle.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public EventWaitHandle(bool initialState, EventResetMode mode, string name)
5050
#if PLATFORM_UNIX
5151
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
5252
#else
53-
if (System.IO.Path.MaxPath < name.Length)
53+
if (Interop.Kernel32.MAX_PATH < name.Length)
5454
{
55-
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Path.MaxPath), nameof(name));
55+
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
5656
}
5757
#endif
5858
}
@@ -98,9 +98,9 @@ internal unsafe EventWaitHandle(bool initialState, EventResetMode mode, string n
9898
#if PLATFORM_UNIX
9999
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
100100
#else
101-
if (System.IO.Path.MaxPath < name.Length)
101+
if (Interop.Kernel32.MAX_PATH < name.Length)
102102
{
103-
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Path.MaxPath), nameof(name));
103+
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
104104
}
105105
#endif
106106
}
@@ -184,9 +184,9 @@ private static OpenExistingResult OpenExistingWorker(string name, EventWaitHandl
184184
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
185185
}
186186

187-
if (null != name && System.IO.Path.MaxPath < name.Length)
187+
if (null != name && Interop.Kernel32.MAX_PATH < name.Length)
188188
{
189-
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Path.MaxPath), nameof(name));
189+
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
190190
}
191191

192192

src/mscorlib/src/System/Threading/Mutex.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ public void ReleaseMutex()
9090
#if !PLATFORM_UNIX
9191
private static void VerifyNameForCreate(string name)
9292
{
93-
if (name != null && (Path.MaxPath < name.Length))
93+
if (name != null && (Interop.Kernel32.MAX_PATH < name.Length))
9494
{
95-
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Path.MaxPath), nameof(name));
95+
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
9696
}
9797
}
9898
#endif
9999

100100
private void CreateMutexCore(bool initiallyOwned, string name, out bool createdNew)
101101
{
102102
#if !PLATFORM_UNIX
103-
Debug.Assert(name == null || name.Length <= Path.MaxPath);
103+
Debug.Assert(name == null || name.Length <= Interop.Kernel32.MAX_PATH);
104104
#endif
105105

106106
uint mutexFlags = initiallyOwned ? Win32Native.CREATE_MUTEX_INITIAL_OWNER : 0;

src/mscorlib/src/System/Threading/Semaphore.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ private static SafeWaitHandle CreateSemaphore(int initialCount, int maximumCount
9393
#if PLATFORM_UNIX
9494
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives);
9595
#else
96-
if (name.Length > Path.MaxPath)
97-
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Path.MaxPath), nameof(name));
96+
if (name.Length > Interop.Kernel32.MAX_PATH)
97+
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
9898
#endif
9999
}
100100

@@ -135,8 +135,8 @@ private static OpenExistingResult OpenExistingWorker(string name, out Semaphore
135135
throw new ArgumentNullException(nameof(name), SR.ArgumentNull_WithParamName);
136136
if (name.Length == 0)
137137
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
138-
if (name.Length > Path.MaxPath)
139-
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Path.MaxPath), nameof(name));
138+
if (name.Length > Interop.Kernel32.MAX_PATH)
139+
throw new ArgumentException(SR.Format(SR.Argument_WaitHandleNameTooLong, name, Interop.Kernel32.MAX_PATH), nameof(name));
140140

141141
//Pass false to OpenSemaphore to prevent inheritedHandles
142142
SafeWaitHandle myHandle = Win32Native.OpenSemaphore(AccessRights, false, name);

src/mscorlib/src/System/TimeZoneInfo.Win32.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,8 @@ private static string TryGetLocalizedNameByMuiNativeResource(string resource)
818818

819819
try
820820
{
821-
StringBuilder fileMuiPath = StringBuilderCache.Acquire(Path.MaxPath);
822-
fileMuiPath.Length = Path.MaxPath;
823-
int fileMuiPathLength = Path.MaxPath;
821+
StringBuilder fileMuiPath = StringBuilderCache.Acquire(Interop.Kernel32.MAX_PATH);
822+
int fileMuiPathLength = Interop.Kernel32.MAX_PATH;
824823
int languageLength = 0;
825824
long enumerator = 0;
826825

0 commit comments

Comments
 (0)