Open file read-only

c#, readonly, winforms

Solution

You need to set the file attributes of the file before you start the process and then set them back when you opened 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.

Problem

In a C# WinForms app, I am using System.IO.Diagnostics.Process.Start(fileName) to open files. The type of file can be .doc, .docx, .xls, .xlsx, .csv, .pdf, or .txt. Is there any way to force these files to be opened read-only?

Original source