简体   繁体   中英

Corrupted XML file due hard reboot in UWP c#

In my code there´s an XML file that i use to get information from it (variables), in diferrent methods, but when there´sa street light issue (something i can´t control) wich makes my device hard reboot the XML file gets corrupted , im trying with this, but it throws an exception in this line " using (fsFileStream = new FileStream( musicLibraryPath... " , help will be really apreciated. I leave my code below:

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.
}

}

but it throws an exception in this line " using (fsFileStream = new FileStream( musicLibraryPath... "

Windows Store apps run sandboxed and have very limited access to the file system. For the most part, they can directly access only their install folder and their application data folder. You cannot use the Path directly. If you want write data to xml file, you could 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.
    }

}

Please note, if you has used music library you need to declare capabilities in the app manifest. For more you could refer Capability .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM