在 UWP 中移动和替换另一个文件后如何修改文件?
How can you modify a file after move and replacing another in UWP?
在 UWP 中调用 MoveAndReplaceAsync
将旧文件替换为新文件后我无法修改新文件。
例如这里我打开一个文件,保存在_file
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
var picker =
new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add(".txt");
_file = await picker.PickSingleFileAsync();
}
我在这个处理程序中写入一个新文件,将其移动并替换到打开的文件上,然后尝试追加。
private async void MoveAndReplaceAndAppend_Click(object sender, RoutedEventArgs e)
{
// Create a temp file for writing to
var tempFolder = ApplicationData.Current.TemporaryFolder;
var filename = Guid.NewGuid().ToString();
var tmpFile = await tempFolder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);
try
{
var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite);
var dw = new DataWriter(stream);
dw.WriteString("New file");
await tmpFile.MoveAndReplaceAsync(_file);
}
catch (Exception ex)
{
var dlg = new MessageDialog($"Error while move and replacing {ex.Message}");
await dlg.ShowAsync();
}
var failures = "";
try
{
await FileIO.AppendTextAsync(_file, "testing");
}
catch (Exception ex)
{
failures += $"Failed appending to _file: {ex.Message}\n";
}
try
{
await FileIO.AppendTextAsync(tmpFile, "testing");
}
catch (Exception ex)
{
failures += $"Failed appending to tmpFile: {ex.Message}\n";
}
var errorDialog = new MessageDialog($"Error while appending\n{failures}");
await errorDialog.ShowAsync();
}
我试过追加旧的和新的 StorageFile
但总是出现以下错误。
Failed appending to _file: The process cannot access the file because it is being used by another process.
Failed appending to tmpFile: The process cannot access the file because it is being used by another process.
问题是前一个流的生命周期在替换操作之前没有结束。您可以使用 Dispose()
手动管理输出流的生命周期。
var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite);
var dw = new DataWriter(stream);
dw.WriteString("New file");
stream.Dispose();
Why don't they close automatically when they go out of scope?
没有必要手动管理输出流的生命周期。您还可以使用 using
语句来管理流的生命周期。
using (var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite))
{
var dw = new DataWriter(stream);
dw.WriteString("New file");
}
当执行离开 using
语句时,流将自动释放。更多可以参考UWP File access permissions.
在 UWP 中调用 MoveAndReplaceAsync
将旧文件替换为新文件后我无法修改新文件。
例如这里我打开一个文件,保存在_file
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
var picker =
new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add(".txt");
_file = await picker.PickSingleFileAsync();
}
我在这个处理程序中写入一个新文件,将其移动并替换到打开的文件上,然后尝试追加。
private async void MoveAndReplaceAndAppend_Click(object sender, RoutedEventArgs e)
{
// Create a temp file for writing to
var tempFolder = ApplicationData.Current.TemporaryFolder;
var filename = Guid.NewGuid().ToString();
var tmpFile = await tempFolder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);
try
{
var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite);
var dw = new DataWriter(stream);
dw.WriteString("New file");
await tmpFile.MoveAndReplaceAsync(_file);
}
catch (Exception ex)
{
var dlg = new MessageDialog($"Error while move and replacing {ex.Message}");
await dlg.ShowAsync();
}
var failures = "";
try
{
await FileIO.AppendTextAsync(_file, "testing");
}
catch (Exception ex)
{
failures += $"Failed appending to _file: {ex.Message}\n";
}
try
{
await FileIO.AppendTextAsync(tmpFile, "testing");
}
catch (Exception ex)
{
failures += $"Failed appending to tmpFile: {ex.Message}\n";
}
var errorDialog = new MessageDialog($"Error while appending\n{failures}");
await errorDialog.ShowAsync();
}
我试过追加旧的和新的 StorageFile
但总是出现以下错误。
Failed appending to _file: The process cannot access the file because it is being used by another process.
Failed appending to tmpFile: The process cannot access the file because it is being used by another process.
问题是前一个流的生命周期在替换操作之前没有结束。您可以使用 Dispose()
手动管理输出流的生命周期。
var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite);
var dw = new DataWriter(stream);
dw.WriteString("New file");
stream.Dispose();
Why don't they close automatically when they go out of scope?
没有必要手动管理输出流的生命周期。您还可以使用 using
语句来管理流的生命周期。
using (var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite))
{
var dw = new DataWriter(stream);
dw.WriteString("New file");
}
当执行离开 using
语句时,流将自动释放。更多可以参考UWP File access permissions.