在 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/