0

Im trying to create an excel file in xamarin forms, populate the file with some user generated data and save that excel file on the user's device so that they can share it with other applications(in my case google drive).

I have checked out microsoft's documentation but it did not really help.

Here is the path im using:

  string filepath =  System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);

also tried

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

but when i execute the program it breaks and says access to path denied.. Ive searched the error multiple times but could not find a solution that worked for me. and yes i have enabled read and write to external storage premissions. Please help me. also if anyone has advice for create excel files in xamarin that would help

Kyle
  • 51
  • 9
  • If you want to access storage of the device in above android 6 version, we need to check runtime permissions before storing data. Here you need to check 'Storage' runtime permissions. – Deepakkumar Apr 05 '21 at 09:30
  • @Deepakkumar the app’s target platform is andriod 8 and higher so this info wont apply to me , but please walk me through how i access runtime permissions in vs2019 xamarin forms, thank you for the reply – Kyle Apr 05 '21 at 11:50
  • https://learn.microsoft.com/en-us/xamarin/essentials/permissions?tabs=ios – Jason Apr 05 '21 at 12:07
  • @kyle https://stackoverflow.com/questions/52442085/error-access-to-the-path-storage-emulated-0-abc-txt-is-denied-in-xamarin-andr/52444922#52444922 – Narendra Sharma Apr 05 '21 at 13:39

1 Answers1

-1

Xamarin.Essentials provides the ability to check and request runtime permissions. You do not to ask for the runtime permission again. Check the MS docs. https://learn.microsoft.com/en-us/xamarin/essentials/permissions?tabs=ios

I tested with the Xamarin.Essentials 1.6.1.

//write

 string text = "12345";
        var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "excel.xlsx");
        using (var writer = File.CreateText(path))
        {
            await writer.WriteLineAsync(text);
        }

//read

 public async Task<string> ReadAsync()
    {
        var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "excel.xlsx");

        if (backingFile == null || !File.Exists(backingFile))
        {
            return null;
        }

        var text = string.Empty;
        using (var reader = new StreamReader(backingFile, true))
        {

            var str = await reader.ReadToEndAsync();

            text = str;

        }

        return text;
    }
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • thank you very very much ! Just one question, it may sound stupid but where on my device/emulatir will the file be saved? i cant seem to find it – Kyle Apr 07 '21 at 04:24
  • i just tried to write a file and got an error, i assume im missing a package but vs does not really specify?? it says cannot resolve system reference please add nuget package or assembly reference(i do have xamarin essentials installed and refrenced) – Kyle Apr 07 '21 at 04:36
  • Xamarin.Essentials is available as a NuGet package and is included in every new project in Visual Studio. If you could not find it, install from NuGet by yourself. https://www.nuget.org/packages/Xamarin.Essentials – Wendy Zang - MSFT Apr 07 '21 at 05:24
  • Environment.SpecialFolder.Personal is internal storage. https://learn.microsoft.com/en-us/xamarin/android/platform/files/ In Internal Storage, you couldn't see the files without root permission. If you want to view it, you could use adb tool. Please check the way in link. https://stackoverflow.com/questions/56966590/how-to-write-the-username-in-a-local-txt-file-when-login-success-and-check-on-fi – Wendy Zang - MSFT Apr 07 '21 at 05:27
  • still get the same error.... xamarin essentails is installed and referenced.. in the project itself i have no errors but as soon as i run the app and click the button to save the file , the program breaks, and gives me that error i mentioned earlier, please help me – Kyle Apr 08 '21 at 15:19
  • I use the latest version of the Xamarin.forms and Xamarin.essentails. Check the version. And if you still get this issue, could you upload the project on GitHub for me to test? – Wendy Zang - MSFT Apr 09 '21 at 01:59
  • yes I will thank you that sounds great ! i will do that after work so in about 10 ours time – Kyle Apr 09 '21 at 04:54
  • uploaded to git hub, repository is called external storage(Dev-ANDRADE) – Kyle Apr 09 '21 at 15:23
  • Could you provide your GitHub project link? – Wendy Zang - MSFT Apr 12 '21 at 08:15
  • i dont know how , im not familiar with github, is there any other way i can share my project – Kyle Apr 13 '21 at 10:46
  • Upload the project on GitHub and provide the project link for me. – Wendy Zang - MSFT Apr 15 '21 at 08:45