我目前正在设计一个程序,每次按下按钮时字符串数组都会保存,并在程序开始时加载。我有两个问题:一,在尝试加载文件时,它命中了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();
}
}
答案 0 :(得分:0)
在程序工作目录中创建一个名为filenamestr
的XML文件。
如果要创建名为filenamestr.xml
的文件,则需要更改using语句。
using (XmlReader reader = XmlReader.Create("filenamestr.xml"))