如何覆盖UWP中的文件? [英] How can I overwrite a file in UWP?

查看:103
本文介绍了如何覆盖UWP中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用FileSavePicker覆盖UWP中的当前文件.我指的是: https://msdn.microsoft.com/zh-cn/windows/uwp/files/quickstart-reading-and-writing-files 但是,当我使用较短的文件覆盖较长的文件时,就会出现问题:旧文件的多余部分(较长)不会被删除,如下所示:

I tried to overwrite a current file in UWP using FileSavePicker. I refered to this: https://msdn.microsoft.com/zh-cn/windows/uwp/files/quickstart-reading-and-writing-files But when I use a shorter file to overwrite a longer file, there comes a problem: the extra part of the old file(longer) isn't deleted, like this:

旧文件:

abcdefg

要保存的文件:

hij

但是保存操作后,我得到了:

But after saving operations, I get this:

hijdefg

我的代码在这里:

        private async void Save_Click(object sender, RoutedEventArgs e)
    {
        XmlDocument Line = SaveToXml();

        //Save it
        FileSavePicker fileSaving = new FileSavePicker();
        fileSaving.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        fileSaving.SuggestedFileName = "NewLine";
        fileSaving.FileTypeChoices.Add("Simple Line Files", new List<string>() { ".Line" });
        StorageFile file = await fileSaving.PickSaveFileAsync();

        if (file != null)
        {
            CachedFileManager.DeferUpdates(file);
            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
            //var stream = await file.OpenStreamForWriteAsync();
            using (var outputStream = stream.GetOutputStreamAt(0))
            {
                using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
                {
                    string xmlDoc = Line.OuterXml;
                    Debug.WriteLine(Line.OuterXml);
                    dataWriter.WriteString(xmlDoc);
                    await dataWriter.StoreAsync();
                    await outputStream.FlushAsync();
                }
            }
            stream.Dispose();
        }
    }

感谢您的帮助!

推荐答案

您的代码var outputStream = stream.GetOutputStreamAt(0)返回文件流中开始位置的输出流. 现在,当您写入文件时,它将从您设置的位置替换文件.当您要写入的新字符串小于文件中的旧字符串时,它不能替换所有旧字符串.因此,使用这种方法,您无法确保可以在旧文件中替换所有字母.

Your code var outputStream = stream.GetOutputStreamAt(0) returns an output stream at the begin location in your file stream. Now when you write to the file, it will replace the file from the location you set. When the new string you want to write is less than the old string in the file, it can not replace all the old string. So using this method, you can't ensure that every letters can be replaced in the old file.

您可以使用 FileIO.WriteTextAsync(IStorageFile, String) 方法将文本写入指定的文件.它可以覆盖旧文件.

You can use FileIO.WriteTextAsync(IStorageFile, String) method to write text to the specified file. It can overwrite the old file.

例如:

private async void Save_Click(object sender, RoutedEventArgs e)
{
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    savePicker.FileTypeChoices.Add("Simple Line Files", new List<string>() { ".line" });
    savePicker.SuggestedFileName = "NewLine";
    StorageFile file = await savePicker.PickSaveFileAsync();
    if (file != null)
    {
        CachedFileManager.DeferUpdates(file);
        await FileIO.WriteTextAsync(file, "hello");
        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
    }
}

这篇关于如何覆盖UWP中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆