gpt4 book ai didi

c# - 发布创建的文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:49:59 24 4
gpt4 key购买 nike

我有以下代码将 Base64 字符串转换为 PDF 文件,保存它然后尝试打开它:

//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);

当文件在 Adob​​e Reader 中打开时,会出现一条消息,指出该文件仍在被另一个应用程序使用。如何让我的应用程序“发布”文件,以便 Adob​​e 可以打开它?请注意这是一个 Windows 8 应用程序。

最佳答案

我怀疑这是问题所在:

var outStream = fs.GetOutputStreamAt(0);

IOutputStream 扩展了 IDisposable,但您并未处理该流。尝试将该部分代码更改为:

using (var outStream = fs.GetOutputStreamAt(0))
{
var dataWriter = new Windows.Storage.Streams.DataWriter(outStream);
dataWriter.WriteBytes(bytes);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
await outStream.FlushAsync();
}

关于c# - 发布创建的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15041854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com