Kobus Botha

Kobus Botha

  • NA
  • 10
  • 1.6k

Protecting files when opening from application

Jul 30 2014 7:47 AM

Good day

I am using Visual C# 2010 and SQL Server 2008 to develop an application to manage our various protocols and Standard Operating Procedures at work. I work in the Cancer department of a hospital.

I have the following basic structure in the database. A Category table, Protocol table (with foreign key to Category table), ProtocolVersion table (with foreign key to the Protocol table), FileDocument table (with foreign key to the ProtocolVersion table). The FileDocument table will hold the filenames of the documents and supporting documents for a particular version of the protocols. At the moment I am just using a file system for the files (i.e. the files still sit in a folder and just the paths are recorded in the database). I will explore and investigate using the SQL Server FileStream in future.

Each protocol will have many versions and each version has a particular life span after which time the protocol needs to be reviewed and updated.

The importing of supporting documents (.pdf, word, excel, etc.) into the program is started by dragging the files onto a protocol node in the treeview. When the file is imported the application copies the file to a application file folder that I specify in the settings and then also inserts the path and filename in the FileDocuments table.

When the user selects a file and click the button to open the file the following code is run (selectedDocument is an object which is instantiated and initialised with the data from the FileDocuments record):

 string fullFilename = System.IO.Path.Combine(selectedDocument.DocumentPath, selectedDocument.DocumentName); FileInfo fInfo = new FileInfo(fullFilename); if (fInfo.Exists) { System.Diagnostics.Process.Start(fullFilename); }

This works well and opens the file using its default program (e.g. .pdf is opened using Adobe Acrobat, etc.). This opens the file from the uploaded folder location.

However I would like to be able to do more. Once the files for a particular protocol version have been imported and approved, the files must be locked/protected completely. This means that the user can open and view the contents of the file, but must not be able to save any changes to the files of the approved protocol version (and ideally not be able to edit the contents of the file at all). I will allow 'Save As' so that the user can save a copy of the file for his/her own use. I just need to make sure that when they select 'Save As' that the SaveFileDialog location does not default to the application file folder. In fact I would like this folder to not be accessible directly by the users at all. If the file needs to be changed, the user needs to change the original file and import into the program again under a new version.

I am trying a few solutions: The first is to make sure the file is read-only when the application opens the file using the following code:

 string fullFilename = System.IO.Path.Combine(selectedDocument.DocumentPath,         selectedDocument.DocumentName); FileInfo fInfo = new FileInfo(fullFilename); if (fInfo.Exists) { //enforce readonly         fInfo.IsReadOnly = true; System.Diagnostics.Process.Start(fInfo.FullName); }
 
A second option is to display the files directly on a control in my application. One problem with this is that if I have dynamic files (like Excel spreadsheet with macros) I will lose the functionality of the file features.

Does anyone have any other ideas or solutions?

Kind regards.