繁体   English   中英

xamarin forms 中的外部存储

[英]External storage in xamarin forms

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).

我已经检查了微软的文档,但它并没有真正帮助。

这是我使用的路径:

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

也试过

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

但是当我执行程序时它会中断并说访问路径被拒绝..我多次搜索错误但找不到适合我的解决方案。 是的,我已经启用了对外部存储权限的读写。 请帮我。 另外,如果有人建议在 xamarin 中创建 excel 文件,这将有所帮助

Xamarin.Essentials 提供了检查和请求运行时权限的能力。 您不要再次请求运行时权限。 检查 MS 文档。 https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=ios

我用 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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM