System.IO.File.OpenRead和System.IO.FileStream之间有什么区别? [英] What is the Difference between System.IO.File.OpenRead AND System.IO.FileStream?

查看:98
本文介绍了System.IO.File.OpenRead和System.IO.FileStream之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的应用程序我正在阅读.PDF文件使用System.IO.FileStream(filePath)。当文件夹具有本地用户权限时,这工作正常。当我从文件夹中删除本地用户时,这会给出访问被拒绝错误。我使用这段代码...



Hello all,
n my application i am reading .PDF file useing System.IO.FileStream (filePath). This is working fine when folder have local user right. When i remove Local user right from folder that time this gives access denied error. I am use this code...

System.IO.FileStream objFStream = new System.IO.FileStream(strPath, System.IO.FileMode.Open);
        byte[] bytRead = new byte[(int)objFStream.Length];
        objFStream.Read(bytRead, 0, (int)objFStream.Length);
        objFStream.Close();
        objFStream.Dispose();







一旦我将System.IO.FileStream替换为System.IO.File.OpenRead(strPath)它会工作。替换代码是..






Once i replace System.IO.FileStream to System.IO.File.OpenRead(strPath) it will work. Replace code is..

System.IO.FileStream objFStream = System.IO.File.OpenRead(strPath);
            byte[] bytRead = new byte[(int)objFStream.Length];
            objFStream.Read(bytRead, 0, (int)objFStream.Length);
            objFStream.Close();
            objFStream.Dispose();







我想知道它们之间的区别是什么这个?

如果有人知道请帮忙。




I want to know what is the different between this?
If any one know please help.

推荐答案

看起来你已经得到了这个答案:

http://stackoverflow.com/问题/ 27397370 / system-io-file-openread-is-working-but-system-io-filestream-not-working [ ^ ]



祝你好运!
Looks like you already got this answered:
http://stackoverflow.com/questions/27397370/system-io-file-openread-is-working-but-system-io-filestream-is-not-working[^]

Good luck!


可能是因为你没有指定文件访问模式 [ ^ ]在你的流上,它试图打开Read and Write,但你的 File.OpenRead 正在打开以供只读使用。在你的流上尝试 FileAccess.Read
Probably because you have not specified a FileAccess mode[^] on your stream, it tries to open fro Read and Write, but your File.OpenRead is opening for read only. Try FileAccess.Read on your stream.


[public static FileStream OpenRead(string path)]等同于FileStream(String, FileMode,FileAccess,FileShare)构造函数重载,FileMode值为Open,FileAccess值为Read,FileShare值为Read。



参见文档 [ ^ ]这里..
[public static FileStream OpenRead(string path)] is equivalent to the FileStream(String, FileMode, FileAccess, FileShare) constructor overload with a FileMode value of Open, a FileAccess value of Read and a FileShare value of Read.

See Documentation[^] Here..


这篇关于System.IO.File.OpenRead和System.IO.FileStream之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆