I used Unity3d(2017.4.1 f1) to export my app to VS2017,but I found that a .xml document(in the directory /Asset/.xml) wasn't exported.So I copied the xml document to /app/data.Then I debugged again,the error happened,it told me I didn't have a authorization to access the data.So how can I solve this problem?Can I change a directory and which directory can I read?
Error
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)
My code
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;}
And I have read MS document for UWP,but I am not sure where my xml and the appdatapath are. There are my test codes
//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\Users\96385\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()
I don't know where my solar.xml is and ApplicationData.Current.LocalFolder is C:\Data\Users\96385\AppData\Local\Packages\BeautifulSolar_pzq3xp76mxafg\LocalState
.The location of solar.xml in VS are /App and /App/data(I copied two) The figurein /data/How can I read my xml?
You should use StreamingAssets folder (Assets/StreamingAssets - create it if it doesn't exist) to include extra XML files in your build.
Copying files manually after a build could be a bad solution, as it's easy to forget later, and will lead to permissions/manifest issues like you're seeing.
To get hold of the file you put into StreamingAssets from a script, you can use File.ReadAllBytes with Application.streamingAssetsPath.
For example:
string fileURI = Path.Combine(Application.streamingAssetsPath, "myFile.xml");
byte[] xmlAsBytes = File.ReadAllBytes(fileURI);
User contributions licensed under CC BY-SA 3.0