一个关于Hololens访问数据权限的问题

A problem about Hololens authorization of accessing data

有关应用程序数据访问的错误失败

我使用 Unity3d(2017.4.1 f1) 将我的应用程序导出到 VS2017,但我发现一个 .xml 文档(在目录 /Asset/ .xml) 不是exported.So 我把xml文件复制到/app/data.然后我再调试,报错了,提示我没有权限访问 data.So 我该如何解决这个问题?我可以更改目录吗?我可以读取哪个目录?

错误

    Exception thrown: 'System.UnauthorizedAccessException' in System.IO.FileSystem.dll
UnauthorizedAccessException: Access to the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\BeautifulSolarVS.Debug_x86.hp\Data\solar.xml' is denied.
   at System.IO.Win32FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs)
   at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode)
   at OperationAll.QueryXml(String xmlname, String queryingname)
   at OperationAll.HoloToolkit.Unity.InputModule.IFocusable.OnFocusEnter()
   at HoloToolkit.Unity.InputModule.InputManager.<>c.<.cctor>b__118_0(IFocusable handler, BaseEventData eventData)
   at UnityEngine.EventSystems.ExecuteEvents.Execute[T](GameObject target, BaseEventData eventData, EventFunction`1 functor)

我的代码

 string QueryXml(string xmlname,string queryingname)
    {
        string filepath = Application.dataPath +@xmlname;
        StreamReader fp = new 
        StreamReader(newFileStream(filepath,FileMode.Open));

    if (File.Exists(filepath))
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(fp);

        XmlNodeList nodelist = xmlDoc.GetElementsByTagName("sample");           
        foreach (XmlElement xe in nodelist)
        {
            if (xe.FirstChild.InnerText == queryingname)
            {
                return xe.LastChild.InnerText;
            }

        }
    }
    return null;
}
void IFocusable.OnFocusEnter()
    {
        this.gameObject.transform.localScale = new Vector3(1.5f, 1.5f,1.5f);
        string ob_selected_name = this.transform.name;
        string ob_attribute = QueryXml(xml_name, ob_selected_name);
        GameObject.Find("Panel1/Display/Text").GetComponent<UnityEngine.UI.Text().text = ob_attribute;}

而且我已经阅读了 MS document for UWP,但我不确定我的 xml 和应用程序数据路径在哪里。 有我的测试代码

//test1
   string QueryXml(string xmlname,string queryingname)
{
    try
    {

        XmlDocument xmlDoc = new XmlDocument();

        var asyncOpt = ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(xmlname);
        asyncOpt.Wait();
        using (var fileStream = asyncOpt.Result)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            using (var reader = XmlReader.Create(fileStream, settings))
            {
                //加载Xml文件
                xmlDoc.Load(reader);
            }
            XmlNodeList nodelist = xmlDoc.GetElementsByTagName("sample");           
            foreach (XmlElement xe in nodelist)
            {
                if (xe.FirstChild.InnerText == queryingname)
                {
                    return xe.LastChild.InnerText;
                }

            }
        }
    }
    catch (System.Exception e)
    {
        Debug.Log("still fail");
        Debug.Log(ApplicationData.Current.LocalFolder.Path);


    }
    return null;

}
Error:

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.IO.FileNotFoundException' in System.Runtime.WindowsRuntime.dll
Exception thrown: 'System.AggregateException' in System.Private.CoreLib.ni.dll
still fail
C:\Data\Users385\AppData\Local\Packages\BeautifulSolar_pzq3xp76mxafg\LocalState

//test2
string QueryXmlTest( string queryingname)
{
    XmlDocument xmlDoc = new XmlDocument();
    StorageFolder docLib = KnownFolders.DocumentsLibrary;
    var docFile = docLib.OpenStreamForReadAsync("Data\solar.xml");
    docFile.Wait();
    var fs = docFile.Result;
    using (fs)
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        using (var reader = XmlReader.Create(fs, settings))
        {
            //加载Xml文件
            //Debug.Log(reader);
            xmlDoc.Load(reader);
        }
        XmlNodeList nodelist = xmlDoc.GetElementsByTagName("sample");
        foreach (XmlElement xe in nodelist)
        {
            if (xe.FirstChild.InnerText == queryingname)
            {
                return xe.LastChild.InnerText;
            }

        }
    }
    return null;
}
Error:Exception thrown: 'System.UnauthorizedAccessException' in Assembly-CSharp.dll
UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   at Windows.Storage.KnownFolders.get_DocumentsLibrary()

不知道我的solar.xml在什么地方,ApplicationData.Current.LocalFolder在C:\Data\Users385\AppData\Local\Packages\BeautifulSolar_pzq3xp76mxafg\LocalState。solar.xml在VS里的位置是/App和/App/data (我抄了两张)图in /data/我的xml怎么看?

in /

您应该使用 StreamingAssets 文件夹(Assets/StreamingAssets - 如果它不存在则创建它)以在您的构建中包含额外的 XML 文件。

在构建后手动复制文件可能是一个糟糕的解决方案,因为以后很容易忘记,并且会导致 permissions/manifest 您所看到的问题。

要获取您从脚本放入 StreamingAssets 的文件,您可以使用 File.ReadAllBytes with Application.streamingAssetsPath

例如:

string fileURI = Path.Combine(Application.streamingAssetsPath, "myFile.xml");
byte[] xmlAsBytes = File.ReadAllBytes(fileURI);