22using System . Threading ;
33using System . Runtime . Versioning ;
44using System . Security . AccessControl ;
5- using System . Collections . Generic ;
5+ using System . Collections . Concurrent ;
66
77namespace System . IO . Abstractions . TestingHelpers ;
88
@@ -37,7 +37,7 @@ public NullFileSystemStream() : base(Null, ".", true)
3737 private readonly FileOptions options ;
3838 private readonly MockFileData fileData ;
3939 private bool disposed ;
40- private static HashSet < string > _fileShareNoneStreams = [ ] ;
40+ private static ConcurrentDictionary < string , byte > _fileShareNoneStreams = [ ] ;
4141
4242 /// <inheritdoc />
4343 public MockFileStream (
@@ -58,7 +58,7 @@ public MockFileStream(
5858 this . path = path ;
5959 this . options = options ;
6060
61- if ( _fileShareNoneStreams . Contains ( path ) )
61+ if ( _fileShareNoneStreams . ContainsKey ( path ) )
6262 {
6363 throw new IOException ( $ "The process cannot access the file '{ path } ' because it is being used by another process.") ;
6464 }
@@ -108,7 +108,7 @@ public MockFileStream(
108108
109109 if ( share is FileShare . None )
110110 {
111- _fileShareNoneStreams . Add ( path ) ;
111+ _fileShareNoneStreams . TryAdd ( path , 0 ) ;
112112 }
113113 this . access = access ;
114114 this . share = share ;
@@ -160,7 +160,7 @@ protected override void Dispose(bool disposing)
160160 }
161161 if ( share is FileShare . None )
162162 {
163- _fileShareNoneStreams . Remove ( path ) ;
163+ _fileShareNoneStreams . TryRemove ( path , out _ ) ;
164164 }
165165 InternalFlush ( ) ;
166166 base . Dispose ( disposing ) ;
0 commit comments