gpt4 book ai didi

.net - 在 .NET Windows 窗体中读取和写入字节数组

转载 作者:行者123 更新时间:2023-12-01 08:36:54 25 4
gpt4 key购买 nike

为什么以下代码不起作用(使用 Word 文档和 PDF 文件测试)?

保存的文件启动了正确的应用程序,但已损坏且无法打开。

OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;

string filename = openFileDialog1.FileName;
FileStream stream = File.OpenRead(filename);
byte[] array = new byte[stream.Length];

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.FileName = filename;
if (DialogResult.OK != saveFileDialog1.ShowDialog())
return;

FileInfo fi = new FileInfo(saveFileDialog1.FileName);
using (FileStream fs = fi.OpenWrite())
{
fs.Write(array, 0, array.Length);
}

最佳答案

使用文件字节数组方法获得更好的结果。

此外,当您使用一次性元素时,请使用 using

        byte[] array;
string filename;

using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;
filename = openFileDialog1.FileName;
array = File.ReadAllBytes(filename);
}

using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
{
saveFileDialog1.FileName = filename;
if (DialogResult.OK != saveFileDialog1.ShowDialog())
return;
File.WriteAllBytes(array);
}

关于.net - 在 .NET Windows 窗体中读取和写入字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696324/

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