1313#if FEATURE_FILESYSTEM_ASYNC
1414using System . Threading ;
1515using System . Threading . Tasks ;
16+ using System . ComponentModel ;
1617#endif
1718
1819// ReSharper disable PossibleMultipleEnumeration
@@ -179,12 +180,9 @@ public void Copy(string sourceFileName, string destFileName)
179180 . New ( sourceFileName )
180181 . CopyTo ( destFileName ) ;
181182 }
182- catch ( UnauthorizedAccessException )
183+ catch ( UnauthorizedAccessException ) when ( _fileSystem . Execute . IsNetFramework )
183184 {
184- _fileSystem . Execute . OnNetFramework ( ( )
185- => throw ExceptionFactory . AccessToPathDenied ( sourceFileName ) ) ;
186-
187- throw ;
185+ throw ExceptionFactory . AccessToPathDenied ( sourceFileName ) ;
188186 }
189187 }
190188
@@ -194,29 +192,28 @@ public void Copy(string sourceFileName, string destFileName, bool overwrite)
194192 using IDisposable registration = RegisterMethod ( nameof ( Copy ) ,
195193 sourceFileName , destFileName , overwrite ) ;
196194
197- _fileSystem . Execute . OnNetFramework (
198- ( ) =>
199- {
200- try
201- {
202- _fileSystem . FileInfo . New ( sourceFileName
203- . EnsureValidFormat ( _fileSystem , nameof ( sourceFileName ) ) )
204- . CopyTo ( destFileName
205- . EnsureValidFormat ( _fileSystem , nameof ( destFileName ) ) ,
206- overwrite ) ;
207- }
208- catch ( UnauthorizedAccessException )
209- {
210- throw ExceptionFactory . AccessToPathDenied ( sourceFileName ) ;
211- }
212- } ,
213- ( ) =>
195+ if ( _fileSystem . Execute . IsNetFramework )
196+ {
197+ try
214198 {
215199 _fileSystem . FileInfo . New ( sourceFileName
216200 . EnsureValidFormat ( _fileSystem , nameof ( sourceFileName ) ) )
217201 . CopyTo ( destFileName
218- . EnsureValidFormat ( _fileSystem , nameof ( destFileName ) ) , overwrite ) ;
219- } ) ;
202+ . EnsureValidFormat ( _fileSystem , nameof ( destFileName ) ) ,
203+ overwrite ) ;
204+ }
205+ catch ( UnauthorizedAccessException )
206+ {
207+ throw ExceptionFactory . AccessToPathDenied ( sourceFileName ) ;
208+ }
209+ }
210+ else
211+ {
212+ _fileSystem . FileInfo . New ( sourceFileName
213+ . EnsureValidFormat ( _fileSystem , nameof ( sourceFileName ) ) )
214+ . CopyTo ( destFileName
215+ . EnsureValidFormat ( _fileSystem , nameof ( destFileName ) ) , overwrite ) ;
216+ }
220217 }
221218
222219 /// <inheritdoc cref="IFile.Create(string)" />
@@ -517,15 +514,18 @@ public DateTime GetLastWriteTimeUtc(SafeFileHandle fileHandle)
517514 public UnixFileMode GetUnixFileMode ( string path )
518515 {
519516 using IDisposable registration = RegisterMethod ( nameof ( GetUnixFileMode ) ,
520- path ) ;
517+ path ) ;
518+
519+ if ( _fileSystem . Execute . IsWindows )
520+ {
521+ throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ;
522+ }
521523
522- return _fileSystem . Execute . OnWindows (
523- ( ) => throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ,
524- ( ) => _fileSystem . Storage . GetContainer (
525- _fileSystem . Storage . GetLocation (
526- path . EnsureValidFormat ( _fileSystem ) )
527- . ThrowExceptionIfNotFound ( _fileSystem ) )
528- . UnixFileMode ) ;
524+ return _fileSystem . Storage . GetContainer (
525+ _fileSystem . Storage . GetLocation (
526+ path . EnsureValidFormat ( _fileSystem ) )
527+ . ThrowExceptionIfNotFound ( _fileSystem ) )
528+ . UnixFileMode ;
529529 }
530530#endif
531531
@@ -537,10 +537,12 @@ public UnixFileMode GetUnixFileMode(SafeFileHandle fileHandle)
537537 using IDisposable registration = RegisterMethod ( nameof ( GetUnixFileMode ) ,
538538 fileHandle ) ;
539539
540- return _fileSystem . Execute . OnWindows (
541- ( ) => throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ,
542- ( ) => GetContainerFromSafeFileHandle ( fileHandle )
543- . UnixFileMode ) ;
540+ if ( _fileSystem . Execute . IsWindows )
541+ {
542+ throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ;
543+ }
544+
545+ return GetContainerFromSafeFileHandle ( fileHandle ) . UnixFileMode ;
544546 }
545547#endif
546548
@@ -683,8 +685,10 @@ public byte[] ReadAllBytes(string path)
683685 FileAccess . Read ,
684686 FileStreamFactoryMock . DefaultShare ) )
685687 {
686- _fileSystem . Execute . NotOnWindows ( ( ) =>
687- container . AdjustTimes ( TimeAdjustments . LastAccessTime ) ) ;
688+ if ( ! _fileSystem . Execute . IsWindows )
689+ {
690+ container . AdjustTimes ( TimeAdjustments . LastAccessTime ) ;
691+ }
688692
689693 return container . GetBytes ( ) . ToArray ( ) ;
690694 }
@@ -769,8 +773,10 @@ public string ReadAllText(string path, Encoding encoding)
769773 FileAccess . Read ,
770774 FileStreamFactoryMock . DefaultShare ) )
771775 {
772- _fileSystem . Execute . NotOnWindows ( ( ) =>
773- container . AdjustTimes ( TimeAdjustments . LastAccessTime ) ) ;
776+ if ( ! _fileSystem . Execute . IsWindows )
777+ {
778+ container . AdjustTimes ( TimeAdjustments . LastAccessTime ) ;
779+ }
774780
775781 using ( MemoryStream ms = new ( container . GetBytes ( ) ) )
776782 using ( StreamReader sr = new ( ms , encoding ) )
@@ -895,10 +901,16 @@ public void Replace(string sourceFileName,
895901 IStorageLocation location =
896902 _fileSystem . Storage . GetLocation ( linkPath
897903 . EnsureValidFormat ( _fileSystem , nameof ( linkPath ) ) ) ;
898- _fileSystem . Execute . OnWindows (
899- ( ) => location . ThrowExceptionIfNotFound ( _fileSystem ) ,
900- ( ) => location . ThrowExceptionIfNotFound ( _fileSystem ,
901- onDirectoryNotFound : ExceptionFactory . FileNotFound ) ) ;
904+ if ( _fileSystem . Execute . IsWindows )
905+ {
906+ location . ThrowExceptionIfNotFound ( _fileSystem ) ;
907+ }
908+ else
909+ {
910+ location . ThrowExceptionIfNotFound ( _fileSystem ,
911+ onDirectoryNotFound : ExceptionFactory . FileNotFound ) ;
912+ }
913+
902914 try
903915 {
904916 IStorageLocation ? targetLocation =
@@ -1087,8 +1099,10 @@ public void SetUnixFileMode(string path, UnixFileMode mode)
10871099 using IDisposable registration = RegisterMethod ( nameof ( SetUnixFileMode ) ,
10881100 path , mode ) ;
10891101
1090- _fileSystem . Execute . OnWindows (
1091- ( ) => throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ) ;
1102+ if ( _fileSystem . Execute . IsWindows )
1103+ {
1104+ throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ;
1105+ }
10921106
10931107 IStorageContainer container = GetContainerFromPath ( path ) ;
10941108 container . UnixFileMode = mode ;
@@ -1103,8 +1117,10 @@ public void SetUnixFileMode(SafeFileHandle fileHandle, UnixFileMode mode)
11031117 using IDisposable registration = RegisterMethod ( nameof ( SetUnixFileMode ) ,
11041118 fileHandle , mode ) ;
11051119
1106- _fileSystem . Execute . OnWindows (
1107- ( ) => throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ) ;
1120+ if ( _fileSystem . Execute . IsWindows )
1121+ {
1122+ throw ExceptionFactory . UnixFileModeNotSupportedOnThisPlatform ( ) ;
1123+ }
11081124
11091125 IStorageContainer container = GetContainerFromSafeFileHandle ( fileHandle ) ;
11101126 container . UnixFileMode = mode ;
@@ -1134,9 +1150,11 @@ public void WriteAllBytes(string path, byte[] bytes)
11341150 throw ExceptionFactory . AccessToPathDenied ( path ) ;
11351151 }
11361152
1137- _fileSystem . Execute . OnWindowsIf (
1138- container . Attributes . HasFlag ( FileAttributes . Hidden ) ,
1139- ( ) => throw ExceptionFactory . AccessToPathDenied ( ) ) ;
1153+ if ( _fileSystem . Execute . IsWindows && container . Attributes . HasFlag ( FileAttributes . Hidden ) )
1154+ {
1155+ throw ExceptionFactory . AccessToPathDenied ( ) ;
1156+ }
1157+
11401158 using ( container . RequestAccess (
11411159 FileAccess . Write ,
11421160 FileStreamFactoryMock . DefaultShare ) )
@@ -1267,9 +1285,12 @@ public void WriteAllText(string path, string? contents, Encoding encoding)
12671285
12681286 if ( contents != null )
12691287 {
1270- _fileSystem . Execute . OnWindowsIf (
1271- container . Attributes . HasFlag ( FileAttributes . Hidden ) ,
1272- ( ) => throw ExceptionFactory . AccessToPathDenied ( ) ) ;
1288+
1289+ if ( _fileSystem . Execute . IsWindows && container . Attributes . HasFlag ( FileAttributes . Hidden ) )
1290+ {
1291+ throw ExceptionFactory . AccessToPathDenied ( ) ;
1292+ }
1293+
12731294 using ( container . RequestAccess (
12741295 FileAccess . Write ,
12751296 FileStreamFactoryMock . DefaultShare ) )
@@ -1326,10 +1347,15 @@ private IStorageContainer GetContainerFromPath(string path,
13261347 IStorageLocation location = _fileSystem . Storage . GetLocation ( path ) ;
13271348 if ( exceptionMode == ExceptionMode . FileNotFoundExceptionOnLinuxAndMac )
13281349 {
1329- _fileSystem . Execute . OnWindows (
1330- ( ) => location . ThrowExceptionIfNotFound ( _fileSystem ) ,
1331- ( ) => location . ThrowExceptionIfNotFound ( _fileSystem ,
1332- onDirectoryNotFound : ExceptionFactory . FileNotFound ) ) ;
1350+ if ( _fileSystem . Execute . IsWindows )
1351+ {
1352+ location . ThrowExceptionIfNotFound ( _fileSystem ) ;
1353+ }
1354+ else
1355+ {
1356+ location . ThrowExceptionIfNotFound ( _fileSystem ,
1357+ onDirectoryNotFound : ExceptionFactory . FileNotFound ) ;
1358+ }
13331359 }
13341360
13351361 if ( exceptionMode == ExceptionMode . Default )
0 commit comments