【问题标题】:Xamarin Forms - File SystemXamarin 表单 - 文件系统
【发布时间】:2019-09-19 19:39:10
【问题描述】:

我正在开发一个跨平台应用程序(UWP - 目标版本 16299、Android 目标版本 Pie 和 iOS;Visual Studio 2019 - Xamarin.Forms 4.1.0),它需要与在 AppData 中创建的本地数据库文件进行通信。在我尝试将信息导出到 AppData 之外的另一个文件之前,一切都很有趣。 我尝试了很多事情都没有成功,我很好奇为什么它对你有用而不对我有用。

这是我测试过的最新代码,与其他代码相似,结果相同:
抛出异常:System.Private.CoreLib.dll 中的“System.UnauthorizedAccessException

using Plugin.FilePicker;
using Plugin.FilePicker.Abstractions;
using Plugin.Permissions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;

namespace Testing
{

    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private Plugin.FilePicker
            .Abstractions.FileData file;


        private async void ImportBtn_Clicked(object sender, EventArgs e)
        {
            try
            {
                file = await CrossFilePicker.Current.PickFile();


            }
            catch (Exception ex)
            {

            }

            file.FileName = "rooms.jpg";
            file.FilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            await CrossFilePicker.Current.SaveFile(file);
        }

        private void ExportBtn_Clicked(object sender, EventArgs e)
        {

            {
                string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string localFilename = "download.txt";
                string localPath = Path.Combine(documentsPath, localFilename);

                try 
                {  
                   File.Create(localPath); 
                }
                catch(Exception ex) 
                { Debug.WriteLine(ex); }
                Debug.WriteLine(localPath);

            }

        }
    }
}

我想提一下,所有功能都已勾选,在拼命尝试让某些东西正常工作时,import_clicked 按预期工作,尝试的文件夹(个人、音乐、图片、共享)是空的。

【问题讨论】:

  • 你用哪个操作系统测试它?
  • Windows 10 专业版
  • 我的意思是移动操作系统 :)
  • @iSpain17 Android 9.0 - API 28。从 Windows 10 模拟。它不会抛出错误,但位置无法访问 [0:] /data/user/0/com.companyname.testing /files/download.txt

标签: c# xamarin.forms unauthorizedaccessexcepti


【解决方案1】:

在本地项目中测试 ExportBtn_Clicked 代码后。它发生同样的错误。

抛出异常:System.Private.CoreLib.dll 中的“System.UnauthorizedAccessException”

System.UnauthorizedAccessException:对路径“C:\Users\xxx\Documents\download.txt”的访问被拒绝。 在 System.IO.FileStream.ValidateFileHandle(SafeFileHandle 文件句柄) 在 System.IO.FileStream.CreateFileOpenHandle(FileMode 模式、FileShare 共享、FileOptions 选项) 在 System.IO.FileStream..ctor(字符串路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、FileOptions 选项) 在 System.IO.File.Create(字符串路径) 在 App8.MainPage..ctor()

从应用程序外部访问文件时,UWP 中会出现此问题。然后我找到了关于这个错误的类似讨论。

Access to the path 'C:\Sites\content\ServerIpAddress.txt' is denied

那我参考File access permissions,问题是UWP要获取应用范围之外的文件,需要使用Windows.Storage。如果有必要使用Documents文件夹,你可以参考这个来访问它。

Accessing additional locations

using Windows.Storage;
var x = await StorageFolder.GetFolderFromPathAsync(@"C:\Users\UserName\Documents"); await x.CreateFileAsync("newfile.txt");

注意:最好在应用程序中使用沙箱文件夹,这将具有完全权限。

Android中,如果想用External storage实现如下:

 Java.IO.File sdCard = Android.OS.Environment.ExternalStorageDirectory;
 Java.IO.File dir = new Java.IO.File (sdCard.AbsolutePath + "/MyFolder");
 dir.Mkdirs ();
 Java.IO.File file = new Java.IO.File (dir,"download.txt");
    if (!file.Exists ()) {
        file.CreateNewFile ();
        file.Mkdir ();
        FileWriter writer = new FileWriter (file);
        // Writes the content to the file
        writer.Write (jsonData);
        writer.Flush ();
        writer.Close ();
    }

【讨论】:

  • Environment.SpecialFolder 转发的所有文件夹都是只读的。是的,解决方法似乎是一项允许我使用 Windows.Storage 的依赖服务。 System.IO 似乎不允许我使用 AppData 之外的任何文件夹。 var x = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(@"C:\Users\UserName\Documents"); await x.CreateFileAsync("newfile.txt"); 这适用于 UWP。 (权限未更改)
  • @andreizet 是的,你是对的。 Environment.SpecialFolder 转发的所有文件夹都是只读的。我更新了答案。最后一个原因是 UWP 从应用程序外部访问文件的限制导致了它。这里用File不能实现,应该用Windows.Storage
  • 是的,我正在使用依赖服务来构建我的解决方案。完成后我将分享我的代码。正在考虑导出到 iOS 版 iCloud,为此我正在等待高层的 Apple 开发者用户访问这些功能。
  • 如果你编辑并添加 Java.IO 作为 Android 的解决方案,它正在做同样的事情,我会投票给你的答案。奇怪的是我们有一个跨平台的库,但仍然必须使用依赖服务。
  • @andreizet 你的意思是在 android 中,我已经更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
相关资源
最近更新 更多