为什么不能使用以下代码(使用Word文档和PDF文件进行测试)?在.NET中读取和写入字节数组Windows Forms
保存的文件将启动正确的应用程序,但已损坏,无法打开。
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);
}
你想做什么? 你有没有尝试用一些文本编辑器打开文件,看看它们包含什么? – 2012-01-01 23:37:58
请包含您遇到的错误或例外文本,并在其出现的位置指明代码行。 – rfmodulator 2012-01-01 23:42:51
用十六进制编辑器打开保存的文件,并查看它们包含的内容。 – 2012-01-01 23:48:54