c# - 以只读方式打开文件

标签 c# winforms readonly

在 C# WinForms 应用程序中,我使用 System.IO.Diagnostics.Process.Start(fileName) 打开文件。文件类型可以是 .doc、.docx、.xls、.xlsx、.csv、.pdf 或 .txt。

有没有办法强制这些文件以只读方式打开?

最佳答案

您需要在开始进程之前设置文件的文件属性,然后在打开它时将其设置回来。

例子:

var attributes = File.GetAttributes(path);

File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly);

System.IO.Diagnostics.Process.Start(fileName);

File.SetAttributes(filePath, attributes);

注意:这将更改实际 文件的文件属性,因此请牢记这一点。

关于c# - 以只读方式打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1626083/

相关文章:

c# - 事件处理程序性能

c++ - 为什么不能给指针赋值?

c# - 在 C# 中设置文件权限

c# - DateTime.ToUniversalTime() 如何工作?

c# - 为什么我的 PNG 纹理不透明?

c# - Process.HasExited 对当前进程是否成立?

mysql - 为什么下面的代码中的日期没有增加?

C# 笔.DashPattern

.net - .NET中是否有只读通用字典?

c# - 只读更改结构的行为