UWP中的硬重启导致XML文件损坏c#

时间:2018-04-07 00:14:26

标签: c# xml uwp windows-10 windows-10-universal

在我的代码中有一个XML文件,用于从中获取信息(变量),使用不同的方法,但是当有路灯问题(我无法控制)时,我的设备很难重启 XML文件损坏,即时尝试使用此功能,但在此行中引发异常“using(fsFileStream =         新的FileStream(musicLibraryPath ......“,帮助将真正贬值。我将代码留在下面:

public async void fileExist(string fileName)
{


 try
{
    //Creates "file.xml".
    StorageFile newBlankDocument =
                await KnownFolders.MusicLibrary.CreateFileAsync(blankFile, CreationCollisionOption.FailIfExists);
}
catch (Exception)
{

}

try
{
    //Creates "configFile.xml".
    StorageFile newDocument = 
        await KnownFolders.MusicLibrary.CreateFileAsync(fileName, CreationCollisionOption.FailIfExists);
    //Gets the file
    StorageFile fileDocument = 
        await KnownFolders.MusicLibrary.GetFileAsync(fileName);

    var musicLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music);
    String musicLibraryPath = musicLibrary.SaveFolder.Path;

    using (fsFileStream = 
        new FileStream( musicLibraryPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1024, FileOptions.WriteThrough))
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = false;

        using (XmlWriter writer = XmlWriter.Create(fsFileStream, settings))
        {
            //Create all the XML document fields.
            writer.WriteStartDocument();
            writer.WriteStartElement("Config");
            writer.WriteStartElement("General");
            writer.WriteAttributeString("name", "DATA");
            writer.WriteStartElement("Local");
            writer.WriteElementString("something1", "");
            writer.WriteElementString("something2", "");
            writer.WriteElementString("something3", "");

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Dispose();
            showReferenceWarning();
            timerReferenceWarning.Start();
        }
    }
}
catch (Exception)
{
    //The file already exist and doesn´t need to be created again.
}

}

1 个答案:

答案 0 :(得分:0)

  

但它在这行中引发异常“using(fsFileStream = new FileStream(musicLibraryPath ...”

Windows应用商店应用运行沙盒,并且对文件系统的访问权限非常有限。在大多数情况下,他们只能直接访问他们的安装文件夹和他们的应用程序数据文件夹。您无法直接使用Path。如果要将数据写入xml文件,可以await fileDocument.OpenAsync(FileAccessMode.ReadWrite)

public async void fileExist(string fileName)
{
    try
    {
        //Creates "file.xml".
        StorageFile newBlankDocument = await KnownFolders.MusicLibrary.CreateFileAsync("file.xml", CreationCollisionOption.FailIfExists);
    }
    catch (Exception)
    {

    }
    try
    {
        //Creates "configFile.xml".
        StorageFile newDocument = await KnownFolders.MusicLibrary.CreateFileAsync(fileName, CreationCollisionOption.FailIfExists);
        //Gets the file
        StorageFile fileDocument = await KnownFolders.MusicLibrary.GetFileAsync(fileName);

        await Task.Run(async () =>
        {
            using (var fsFileStream = await fileDocument.OpenAsync(FileAccessMode.ReadWrite))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = false;

                using (XmlWriter writer = XmlWriter.Create(fsFileStream.AsStreamForWrite(), settings))
                {
                    //Create all the XML document fields.
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Config");
                    writer.WriteStartElement("General");
                    writer.WriteAttributeString("name", "DATA");
                    writer.WriteStartElement("Local");
                    writer.WriteElementString("something1", "");
                    writer.WriteElementString("something2", "");
                    writer.WriteElementString("something3", "");
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                    writer.Dispose();
                }
            }
        });

    }
    catch (Exception ex)
    {            
        //The file already exist and doesn´t need to be created again.
    }

}

请注意,如果您使用过音乐库,则需要在应用清单中声明功能。有关更多信息,请参阅Capability