发布创建的文件 [英] Release Created File
本文介绍了发布创建的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码将 Base64 字符串转换为 PDF 文件,保存它然后尝试打开它:
I've got the following code that converts a Base64 string to a PDF file, saves it then tries to open it:
//Converts the Base64 data to bytes
byte[] bytes = Convert.FromBase64String(Base64String);
//Stores the converted Base64 data in the application's Local Resource directory, in PDF format
StorageFolder folder = ApplicationData.Current.LocalFolder;
string fileName = file.pdf";
CreationCollisionOption options = CreationCollisionOption.FailIfExists;
var file = await folder.CreateFileAsync(fileName, options);
var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
var outStream = fs.GetOutputStreamAt(0);
var dataWriter = new Windows.Storage.Streams.DataWriter(outStream);
dataWriter.WriteBytes(bytes);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
await outStream.FlushAsync();
string pdfPath = folder.Path + "\\" + fileName;
StorageFile fileToLaunch = await StorageFile.GetFileFromPathAsync(pdfPath);
await Windows.System.Launcher.LaunchFileAsync(fileToLaunch);
在 Adobe 阅读器中打开文件时,会显示一条消息,指出该文件仍在被其他应用程序使用.如何让我的应用程序发布"文件,以便 Adobe 可以打开它?请注意,这是一个 Windows 8 应用程序.
When the file opens in Adobe reader, there is a message saying the file is still in use by another application. How do I get my application to 'release' the file, so Adobe can open it? Please note this is a Windows 8 app.
推荐答案
我不得不将代码更改为以下内容:
I had to change the code to the following:
var file = await folder.CreateFileAsync(fileName, options);
using (var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
using (var outStream = fs.GetOutputStreamAt(0))
{
using (var dataWriter = new Windows.Storage.Streams.DataWriter(outStream))
{
dataWriter.WriteBytes(bytes);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
await outStream.FlushAsync();
}
}
}
这篇关于发布创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文