Bug 33007 - File.Delete should fail while file is used by a FileStream
Summary: File.Delete should fail while file is used by a FileStream
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-08-12 12:07 UTC by marcos.henrich
Modified: 2015-08-17 10:37 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description marcos.henrich 2015-08-12 12:07:56 UTC
string fileName = "tmp";
using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
 File.Delete (fileName);

On .Net the code above throws:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\Users\Marcos\Documents\tmp.txt' because it is being used by another process.

On Mono no exception occurs.

This reproduces a failing assert on one of the Roslyn tests.
https://github.com/dotnet/roslyn/blob/39a66a8db9ceed720e992b9ba04a08bf1544bc50/src/Scripting/Test/MetadataShadowCopyProviderTests.cs#L156
Comment 1 Marek Safar 2015-08-17 10:37:47 UTC
I don't think this is Mono issue. Windows does not allow you do that but that does not mean other systems have same limitation. 

Looking at the windows exception, it does not seem to indicate any .net code is involved

Unhandled Exception: System.IO.IOException: The process cannot access the file 'c:\git\mono\mcs\mcs\tmp' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalDelete(String path, Boolean checkHost)
   at ExceptionDispatchInfoRepro.MainClass.Main(String[] args)