Bug 654 - UnauthorizedAccessException when File.Delete()ing an external file
Summary: UnauthorizedAccessException when File.Delete()ing an external file
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-09-06 14:45 UTC by Jonathan Pryor
Modified: 2011-09-06 20:27 UTC (History)
2 users (show)

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

ScratchFileDelete.zip (11.43 KB, application/zip)
2011-09-06 14:45 UTC, Jonathan Pryor

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 Developer Community or GitHub 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:

Description Jonathan Pryor 2011-09-06 14:45:19 UTC
Created attachment 256 [details]

I get an System.UnauthorizedAccessException when using File.Delete() to delete a file located on external storage. Removing a file on internal storage appears to work normally.

1. Extract the attached project.
2. Build, Install, Run the app (ScratchFileDelete).
3. Click the 'Delete file' button.
4. App crashes:

I/MonoDroid(23105): UNHANDLED EXCEPTION: System.UnauthorizedAccessException: Access to the path "/mnt/sdcard/Android/data/ScratchFileDelete.ScratchFileDelete/files/gps-info.txt" is denied.
I/MonoDroid(23105): at System.IO.FileStream..ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,bool,System.IO.FileOptions) <IL 0x00376, 0x00650>
I/MonoDroid(23105): at System.IO.FileStream..ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,bool,bool) <IL 0x0001c, 0x000bb>
I/MonoDroid(23105): at System.IO.FileStream..ctor (string,System.IO.FileMode,System.IO.FileAccess) <IL 0x00019, 0x00063>
I/MonoDroid(23105): at ScratchFileDelete.Activity1.OnCreate (Android.OS.Bundle) <IL 0x00266, 0x00857>
I/MonoDroid(23105): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00012, 0x00057>
I/MonoDroid(23105): at (wrapper dynamic-method) object.c07260ad-f0a5-4d1c-b9d4-148fe852eb6d (intptr,intptr,intptr) <IL 0x00012, 0x00033>

5. Expected behavior: no exception.

As a workaround, you can P/Invoke to unlink(2). The 'Unlink file' button does this, and it works without error (the unlink() return value is 0).
Comment 1 Jonathan Pryor 2011-09-06 14:46:10 UTC
I don't think this has anything to do with file sharing, for two reasons:

1. I ensure that the FileStream is disposed of before deleting the file (`using` block).
2. I specify FileShare.ReadWrite in the FileStream constructor (so file sharing should be allowed anyway).
Comment 2 Rodrigo Kumpera 2011-09-06 20:27:14 UTC
Fixed on mobile-master.