how to delete files on internal or external storage in Maui:Andorid

john murray 11 Reputation points
2022-07-02T15:56:22.827+00:00

I have seen a couple of posts related to this and it happens on Xamarin Forms similarly

1) open a file picker
2) read file process data
3) delete file

   var res = await PickAndShowAsync(opt);  
           if (res == "") //user cancelled the file picker  
           { return; }  
           doprocess(dbpath);   // this opens file reads and processes data closes file; - successfully  


    if (File.Exists(cdbpath))      // dbpath is fully qualified folder and filename usually file is on attached usb sd card reader   
                                             // but I have set up a similar arrangement picking file from internal storage   
                       {  
                         var fh =   File.Open(csvorig,FileMode.Open);  
                           fh.Close( );  
                             try{  
                             File.Delete(fbpath);  
                            }catch(Exception ee)        never catches any exception but does not delete the file  
                           {  await DisplayAlert("warn",ee.Message,"cancel")}  

Unfortunately it takes the directive File.Delete(myfile) but does not delete NOR does it throw any exceptions
I have tried File.Exists - returns true as well as opening and closing the file (obviously it is opened for reading and data processing but it is closed before attempting to delete

NOTE the windows version of this deletes files happily, the Android versions do not
Yes of course the permissions are set READ_EXTERNAL WRITE_EXTERNAL READ_INTERNAL etc etc

I can see from 1 post that others have experienced this issue but the proposed answers involve Android context calls and elaborate code requesting permissions which cause huge numbers of errors or using a dependency interface which MSDN docs say should be necessary

the manifest is as follows

   <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>  
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
   <uses-permission android:name="android.permission.INTERNET" />  
     <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />>  
           <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />  
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
     <uses-permission android:name="android.permission.MANAGE_USB" />  
     <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />  
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />  

I would be very grateful if anyone can point me to an answer - I am returning to use Maui after using Xamarin some years ago and I cant get my head around how complex its become

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,977 questions
{count} vote