Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hii friends

I actually want to open files(.txt, .pdf,.xls) in read only. The files should just be read but shdn`t allow them to be edited.

Can any body suggest how it can be done using Windows Form Application (C#)

Thanks & Regards
Sandy
Posted
Updated 21-Jun-18 20:34pm

System.IO.FileInfo fileObj = new System.IO.FileInfo("C:\temp\readme.txt");
fileObj.Attributes = System.IO.FileAttributes.ReadOnly;
System.Diagnostics.Process.Start(fileObj.FullName);
 
Share this answer
 
For example, is you open a file with System.IO.StreamReader, the file will be opened as read-only and by default it will be without shared access, so the same file cannot be opened in other process in no mode, read-only or read-write.

I think you're confused; what do you want to achieve (please don't repeat about file security; the ultimate goals, please)?

—SA
 
Share this answer
 
Comments
JF2015 19-Apr-11 1:50am    
5ed - quite good, but OP does obviously not know what he really wants. - Happens so often...
Sergey Alexandrovich Kryukov 19-Apr-11 1:51am    
True. Thank you very much.
--SA
This is not a problem of opening the file in readonly mode, it is a question of what you do with the data that you read from the file. For example when opening a textfile (*.txt) you could display the content in a Readonly Textbox - this wouldn't allow anyone to change the content.
 
Share this answer
 
Comments
Sandy058 19-Apr-11 0:54am    
Hii,
Yeah I agree with that but we cannot display a .pdf contents into a textbox.. Is there any method to provide security to a particular file.. security in sence... just read only mode...
Sergey Alexandrovich Kryukov 19-Apr-11 1:42am    
Read-only does not provide any kind of security. What do you want to achieve?
--SA
Sergey Alexandrovich Kryukov 19-Apr-11 1:43am    
What happened to you lately. I need to compensate for most stupid down-votes...
My 5 this time; this is only because the answer is correct, not just to compensate...
--SA
Sergey Alexandrovich Kryukov 19-Apr-11 1:47am    
I added my answer, please see...
--SA

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