@@ -249,29 +249,40 @@ public FileStream(
249249 ( int ) IOException . IOExceptionErrorCode . UnauthorizedAccess ) ;
250250 }
251251
252+ // a read-only file can't be opened with write access
253+ // (CreateNew is left to report that the file already exists, as in full .NET)
254+ if ( isReadOnly
255+ && wantsWrite
256+ && mode != FileMode . CreateNew )
257+ {
258+ throw new IOException (
259+ string . Empty ,
260+ ( int ) IOException . IOExceptionErrorCode . UnauthorizedAccess ) ;
261+ }
262+
252263 // The seek limit is 0 (the beginning of the file) for all modes except Append
253264 _seekLimit = 0 ;
254265
255266 switch ( mode )
256267 {
257268 case FileMode . CreateNew :
258- CreateNewFile ( exists , bufferSize ) ;
269+ CreateNewFile ( exists , access , bufferSize ) ;
259270 break ;
260271
261272 case FileMode . Create :
262- CreateFile ( exists , bufferSize ) ;
273+ CreateFile ( exists , access , bufferSize ) ;
263274 break ;
264275
265276 case FileMode . Open :
266- OpenFile ( exists , bufferSize ) ;
277+ OpenFile ( exists , access , bufferSize ) ;
267278 break ;
268279
269280 case FileMode . OpenOrCreate :
270- OpenOrCreateFile ( bufferSize ) ;
281+ OpenOrCreateFile ( access , bufferSize ) ;
271282 break ;
272283
273284 case FileMode . Truncate :
274- TruncateFile ( exists , bufferSize ) ;
285+ TruncateFile ( exists , access , bufferSize ) ;
275286 break ;
276287
277288 case FileMode . Append :
@@ -636,6 +647,7 @@ private void RegisterShareInformation(
636647
637648 private void CreateNewFile (
638649 bool exists ,
650+ FileAccess access ,
639651 int bufferSize )
640652 {
641653 // if the file exists, IOException is thrown
@@ -648,17 +660,20 @@ private void CreateNewFile(
648660
649661 _nativeFileStream = new NativeFileStream (
650662 _fileName ,
651- bufferSize ) ;
663+ bufferSize ,
664+ ( int ) access ) ;
652665 }
653666
654667 private void CreateFile (
655668 bool exists ,
669+ FileAccess access ,
656670 int bufferSize )
657671 {
658672 // if the file exists, it should be overwritten
659673 _nativeFileStream = new NativeFileStream (
660674 _fileName ,
661- bufferSize ) ;
675+ bufferSize ,
676+ ( int ) access ) ;
662677
663678 if ( exists )
664679 {
@@ -668,6 +683,7 @@ private void CreateFile(
668683
669684 private void OpenFile (
670685 bool exists ,
686+ FileAccess access ,
671687 int bufferSize )
672688 {
673689 // if the file does not exist, IOException/FileNotFound is thrown
@@ -680,19 +696,24 @@ private void OpenFile(
680696
681697 _nativeFileStream = new NativeFileStream (
682698 _fileName ,
683- bufferSize ) ;
699+ bufferSize ,
700+ ( int ) access ) ;
684701 }
685702
686- private void OpenOrCreateFile ( int bufferSize )
703+ private void OpenOrCreateFile (
704+ FileAccess access ,
705+ int bufferSize )
687706 {
688707 // if the file does not exist, it is created
689708 _nativeFileStream = new NativeFileStream (
690709 _fileName ,
691- bufferSize ) ;
710+ bufferSize ,
711+ ( int ) access ) ;
692712 }
693713
694714 private void TruncateFile (
695715 bool exists ,
716+ FileAccess access ,
696717 int bufferSize )
697718 {
698719 // the file would be overwritten. if the file does not exist, IOException/FileNotFound is thrown
@@ -705,7 +726,8 @@ private void TruncateFile(
705726
706727 _nativeFileStream = new NativeFileStream (
707728 _fileName ,
708- bufferSize ) ;
729+ bufferSize ,
730+ ( int ) access ) ;
709731
710732 _nativeFileStream . SetLength ( 0 ) ;
711733 }
@@ -723,7 +745,8 @@ private void AppendToFile(
723745
724746 _nativeFileStream = new NativeFileStream (
725747 _fileName ,
726- bufferSize ) ;
748+ bufferSize ,
749+ ( int ) access ) ;
727750
728751 _seekLimit = _nativeFileStream . Seek (
729752 0 ,
0 commit comments