System.UnauthorizedAccessException:拒绝访问路径“filepath”

时间:2018-02-05 12:06:52

标签: c# android xamarin storage

我遇到了在SD卡上写文件的问题。在这里,我打开一个流来写入数据。

public Stream GetStreamWrite(string filePath) // filePath = "/storage/B4AD-3FC1/Xml/kek.xml"
{ // B4AD-3FC1 - name of sd card
    var write = File.OpenWrite(filePath); // Here I get UnauthorizedAccessException  Access to the path is denied
    return write;
}

这个问题可能是什么原因?

异常堆栈跟踪

System.UnauthorizedAccessException: Access to the path "/storage/B4AD-3FC1/Xml/kek.xml" is denied.
                                           at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0027b] in <a1809d1a7c0741e8b5f4d5285e25edcf>:0 
                                           at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <a1809d1a7c0741e8b5f4d5285e25edcf>:0 
                                             at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
                                           at System.IO.File.OpenWrite (System.String path) [0x00000] in <a1809d1a7c0741e8b5f4d5285e25edcf>:0 
                                           at Dct.Droid.AndroidDataStorage.GetStreamWrite (System.String filePath) [0x00000] in <06e2b936a8ac46408294407357c72652>:0 
                                           at Dct.GoodStorage.GenerateLocalFile (System.String dir, System.String fileName, System.String format, System.String prefix) [0x0029d] in <659575193ffe49819bcb0803dc16e2a1>:0 

P.S。我尝试过File.Create(.NET)和File.CreateNewFile(Java),但都返回相同的异常。

0 个答案:

没有答案