1
我正在尝试读取二进制文件,并且在64位系统上不断收到错误,看起来该文件正在以写入权限打开,并因此在放置在安全文件夹中时引发错误(Program Files in 64 bit视窗)。我可以通过将包含要读取文档的文件夹设置为只读来在我的系统(XP,32位)上复制此错误。C#的FileStream打开具有写权限的文件?
下面是我使用的代码:
public static byte[] GetContentFromFile(string file)
{
try
{
FileStream stream = new FileStream(String.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, file), FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] content = new byte[stream.Length];
reader.Read(content, 0, content.Length);
reader.Close();
stream.Close();
return content;
}
catch
{
return new byte[0];
}
}
任何想法?
它给你什么错误信息? – w69rdy 2010-09-27 15:58:31
您的方法是否执行与File.ReadAllBytes()不同的任何操作? – 2010-09-27 16:03:58
此代码不要求写入权限。我们正在查看正确的代码吗? – 2010-09-27 17:57:49