@@ -94,8 +94,8 @@ static bool IsOneDriveFolder(string folderPath)
9494 public static void PreventSleep ( )
9595 {
9696 PInvoke . SetThreadExecutionState (
97- EXECUTION_STATE . ES_CONTINUOUS |
98- EXECUTION_STATE . ES_SYSTEM_REQUIRED |
97+ EXECUTION_STATE . ES_CONTINUOUS |
98+ EXECUTION_STATE . ES_SYSTEM_REQUIRED |
9999 EXECUTION_STATE . ES_DISPLAY_REQUIRED ) ;
100100 }
101101
@@ -116,31 +116,35 @@ public static void RestoreSleep()
116116
117117
118118
119- public static List < string > AsShortPathNames ( this IEnumerable < string > filesList )
119+ public static IEnumerable < string > AsShortPathNames ( this IEnumerable < string > filesList )
120120 {
121- return filesList
122- . Select ( file => ( file . Length >= 255 ? GetShortPath ( file ) ?? file : file ) )
123- . ToList ( ) ;
121+ return filesList . Select ( file => ( file . Length >= 255 ? GetShortPath ( file ) ?? file : file ) ) ;
124122 }
125123
126124
127- private static string ? GetShortPath ( string filePath )
125+ private static string GetShortPath ( string filePath )
128126 {
129- if ( string . IsNullOrWhiteSpace ( filePath ) ) return null ;
127+ const string LongPathPrefix = @"\\?\" ;
128+ ReadOnlySpan < char > longPathPrefixSpan = LongPathPrefix ;
130129
131- bool addPrefix = filePath . Length >= 255 && ! filePath . StartsWith ( @"\\?\" ) ;
132- if ( addPrefix ) filePath = @"\\?\" + filePath ;
130+ if ( string . IsNullOrWhiteSpace ( filePath ) ) return filePath ;
131+ ReadOnlySpan < char > filePathSpan = filePath ;
133132
134- Span < char > shortPath = stackalloc char [ 1024 ] ;
135- uint res = PInvoke . GetShortPathName ( filePath , shortPath ) ;
136- if ( res == 0 ) return null ;
133+ bool addPrefix = filePathSpan . Length >= 255 && ! filePathSpan . StartsWith ( longPathPrefixSpan , StringComparison . Ordinal ) ;
134+ string pathToUse = addPrefix ? LongPathPrefix + filePath : filePath ;
137135
138- var result = new string ( shortPath . Slice ( 0 , ( int ) res ) ) ;
139- return addPrefix && result . StartsWith ( @"\\?\" ) ? result [ 4 ..] : result ;
136+ Span < char > shortPath = stackalloc char [ 1024 ] ;
137+ uint res = PInvoke . GetShortPathName ( pathToUse , shortPath ) ;
138+ if ( res == 0 ) return filePath ;
140139
140+ ReadOnlySpan < char > resultSpan = shortPath [ ..( int ) res ] ;
141+ return addPrefix && resultSpan . StartsWith ( longPathPrefixSpan , StringComparison . Ordinal )
142+ ? resultSpan [ longPathPrefixSpan . Length ..] . ToString ( )
143+ : resultSpan . ToString ( ) ;
141144 }
142145
143146
147+
144148 public static unsafe uint GetClusterSize ( string folderPath )
145149 {
146150 UInt32 lpSectorsPerCluster ;
@@ -163,7 +167,7 @@ public static unsafe long GetFileSizeOnDisk(string file)
163167 {
164168 uint highOrder ;
165169 uint lowOrder = PInvoke . GetCompressedFileSize ( file , & highOrder ) ;
166- if ( lowOrder == 0xFFFFFFFF && ( Marshal . GetLastWin32Error ( ) != 0 ) ) return - 1 ;
170+ if ( lowOrder == 0xFFFFFFFF && ( Marshal . GetLastWin32Error ( ) != 0 ) ) return - 1 ;
167171 return ( ( long ) highOrder << 32 ) | lowOrder ;
168172 }
169173
@@ -204,7 +208,7 @@ public static bool HasDirectoryWritePermission(string folderName)
204208 return writeAllowed && ! writeDenied ;
205209
206210 }
207- catch ( UnauthorizedAccessException ) { return false ; }
211+ catch ( UnauthorizedAccessException ) { return false ; }
208212 }
209213
210214
0 commit comments