Open read-only file

In a C # WinForms application, I use System.IO.Diagnostics.Process.Start (filename) to open files. The file type can be .doc, .docx, .xls, .xlsx, .csv, .pdf or .txt.

Is there a way to make these files open read-only?

+5
source share
5 answers

Before starting the process, you must set the file attributes and then set them back when you open it.

Example:

var attributes = File.GetAttributes(path);

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

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

File.SetAttributes(filePath, attributes);

Note. This will change the file attributes of the actual file , so keep that in mind.

+11
source

Unfortunately, the way this is done varies with the file type.

ProcessStartInfo.Verbs . "OpenAsReadOnly". ProcessStartInfo.

- , .

+8

, ?

?

+2

Process.Start , . , , , ( OpenAsReadOnly).

, , , , .

+1

, / . , . , Process.Start , .

0

All Articles