使用XmlReader.Create时,不可捕获的FileNotFoundException
本文关键字:FileNotFoundException XmlReader Create 使用 | 更新日期: 2023-09-27 17:51:06
我目前正在设计一个程序,其中字符串数组保存每次按下按钮并在程序开始时加载。我有两个问题:一个,当试图加载文件时,它击中using语句,它给了我System.IO.FileNotFoundException
的例外。后来在按下第三个按钮后,它给了我System.UnauthorizedAccessException
的例外和System.Exception
的崩溃。有什么办法可以解决这个问题吗?注意:这在Windows 8上运行。
public static void excecuteload()
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(string[]));
string[] loadednames;
using (XmlReader reader = XmlReader.Create("filenamestr")) //Causes an exception: System.IO.FileNotFoundException
{
loadednames = (string[])ser.Deserialize(reader);
}
names = (string[])loadednames.Clone();
}
catch (Exception)
{
;
}
}
public async static void excecutesave()
{
try
{
StorageFolder folder = ApplicationData.Current.RoamingFolder;
XmlWriterSettings setstr = new XmlWriterSettings();
setstr.Encoding = Encoding.Unicode;
XmlObjectSerializer serializerstr = new DataContractSerializer(typeof(string[])); //Gives error of System.UnauthorizedAccessException on the third go through
Stream streamstr = await folder.OpenStreamForWriteAsync("filenamestr", CreationCollisionOption.ReplaceExisting);
XmlWriter writerstr = XmlWriter.Create(streamstr, setstr);
serializerstr.WriteObject(writerstr, names);
}
catch (Exception)
{
MessageDialog oops = new MessageDialog("Oops... we couldn't save your score :(");
oops.ShowAsync();
}
}
在程序工作目录中创建一个名为filenamestr
的XML文件。
如果你想创建一个名为filenamestr.xml
的文件,那么你必须修改using语句。
using (XmlReader reader = XmlReader.Create("filenamestr.xml"))