Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

C#
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();



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

C#
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.
Posted

 
Share this answer
 
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.
 
Share this answer
 
[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..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900