如何使用Windows窗体应用程序,C#以只读格式打开文件 [英] How to open a file in Read Only Format using Windows Form Application, C#

查看:319
本文介绍了如何使用Windows窗体应用程序,C#以只读格式打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii朋友



我实际上想以只读方式打开文件(.txt,.pdf,.xls)。应该只读取文件但是shdn`t允许对它们进行编辑。



任何人都可以建议如何使用Windows Form Application(C#)来完成



谢谢&问候

Sandy

解决方案

例如,您是否使用 System.IO.StreamReader打开文件,该文件将以只读方式打开,默认情况下它没有共享访问权限,因此在其他进程中无法以无模式,只读或读写方式打开同一文件。



我觉得你很困惑;你想达到什么目的(请不要重复文件安全;最终目标,请)?



-SA


这不是以只读模式打开文件的问题,而是您对从文件中读取的数据执行的操作的问题。例如,当打开文本文件(* .txt)时,您可以在只读文本框中显示内容 - 这将不允许任何人更改内容。


System.IO.FileInfo fileObj = new System.IO.FileInfo(C:\ temp \readme.txt);

fileObj.Attributes = System.IO.FileAttributes.ReadOnly;

System .Diagnostics.Process.Start(fileObj.FullName);

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

解决方案

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


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.


System.IO.FileInfo fileObj = new System.IO.FileInfo("C:\temp\readme.txt");
fileObj.Attributes = System.IO.FileAttributes.ReadOnly;
System.Diagnostics.Process.Start(fileObj.FullName);


这篇关于如何使用Windows窗体应用程序,C#以只读格式打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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