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?
Locate the file you want to open and then click once on the file name. Click on the down-arrow at the right side of the Open button. Word displays the different ways you can open the file. Choose Open Read-Only from the drop-down menu.
Click the File menu, and then click Passwords. In the File Passwords dialog box, select the Read-only recommended check box, and click OK. Close the workbook. The next time the workbook is opened, a message will ask if you want to open as read-only.
2. Right-click the file (PC) or Control-Click (Mac), and then click Properties. 3. Click the General tab, clear the Read-only check box, and then click OK.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With