none
Intermittent extra characters remains at end of file XML file using AsStreamForWrite

    Question

  • My app uses the code fragment below to write an object to an XML file. It works most of the time just fine. But occasionally there are extra charters at the end of the XML file.  Usually, the app is updating, i.e. writing over the top of the XML file with updated content. And it appears that the "extra characters" are left over from the contents being overwritten. 

    Is there something in addition to this code that should be used to write a new end-of-file marker?

                        IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);
                        using (Stream outStream = Task.Run(() => writeStream.AsStreamForWrite()).Result)
                        {
                            serializer.Serialize(outStream, Obj);
                            outStream.Flush();
                            outStream.Dispose();
                        }
    

    Thanks,

    Rick

    Monday, August 13, 2012 6:23 PM

All replies

  • Use the create flag to overwrite the file:

    app.local.folder.createFileAsync("thefile.xml",Windows.Storage.CreationCollisionOption.replaceExisting).done(function (file) {


    Jeff Sanders (MSFT)

    Tuesday, August 14, 2012 12:46 PM
  • Well, Ialready have a StorageFile object of the file I am replacing. This StorageFile object came from the user picking it from a gridview file list of a user selected directory, i.e. not the App's local storage. My app modifies the user selected file and replaces it on the disk. (My code in the original post works except for not truncating the previous copy of the file.)

    So, simply using the parameter "thefile.xml" doesn't work. I tried "myfile.Name" (where "myfile" is more the StorageFile I mentioned above)

    file = await appData.LocalFolder.CreateFileAsync(myfile.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);

    IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite); using (Stream outStream = Task.Run(() => writeStream.AsStreamForWrite()).Result) { serializer.Serialize(outStream, Obj); outStream.Flush(); outStream.Dispose(); }

    but the file was not replaced. The code ran without any errors, but the file was not updated, not replaced. And, searching using Windows-File-Explorer did not find a new file, so, I don't know where it went. Using "file.Path" causes an exception.

    I found this StorageFile method:

    public static IAsyncOperation<StorageFile> ReplaceWithStreamedFileAsync(
      IStorageFile fileToReplace, 
      StreamedFileDataRequestedHandler dataRequested, 
      IRandomAccessStreamReference thumbnail
    )

    and the "Replace" sounds like it would solve the problem, but, I do not see how to make it work in my code above.

    I suppose I could go back to the "FutureAccessList" for the original directory chosen by the user. But, doesn't seem right when I already have the SorageFile object and I simply want to replace the contents.

    How should this be handled?

    Thanks,

    Rick

    Tuesday, August 14, 2012 3:15 PM
  • Hi,

    You mentioned you're not using local storage. But the code you provided is opening a file from local storage (appData.LocalFolder). It would be better if you can make sure you're writing to the correct file. If you already have a storage file object, please use it and it is not needed to call createFileAsync.  You can also create a temporary file, write to it, and copy it to replace the original file if the write operation succeeds. This helps to ensure the original file is intact if something goes wrong.

    In addition, it seems you're using C# instead of JavaScript. Please post the question on http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/threads.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, August 30, 2012 11:27 AM
  • Hi Rick,

    If you are picking the file to modify, you need to use the file handle that the picker provides. 

    Here is where your file is and why you cannot find it:

    file = await appData.LocalFolder.CreateFileAsync(myfile.Name,

    LocalFolder is your application LocalFolder, and it will have myfile.Name for the name. 

    Does that help?

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, August 30, 2012 11:35 AM