Mark a file as created by my app

117 Views Asked by At

My application creates some pdf files using some private assemblies and then send them to another app by copying them in the second app folders.

After a while I need to come back and get my files and update them, the thing is that meanwhile some other pdf files can be added there which are not my files, and i need to get and use only my pdf files.

My question is, how can I easily identify my files? Should I use a specific name for them? Should I create a file in which to store my file names? Or is there something that can sign a file as "my file" (without using a certificate or a third party software)?

Please note that i am using the last versions of C# and .NET .

3

There are 3 best solutions below

0
On BEST ANSWER

There are actually 3 ways:

  • Write a signature and properties into PDF file and then read back. This requires to use specialized 3rd party PDF library to write, read, add signature to PDF;
  • Maintain a separate XML file with detailed information like last updated, version, CRC or MD5 value to make sure file was not changed. So you have e.g. MyDocument.pdf and MyDocument.pdf.xml and you may search for all .pdf.xml file inside folder and then just change .pdf.xml to .pdf extension to get original pdf filenames. This way won't require any 3rd party components to be implemented. You may also sign XML file to make sure it was not modified.
  • The simplest way is to use filenames to store the additional information and CRC or MD5 hash value (to maintain the integrity). For example, you may name files like "MyApp_15-06-2015-e4d909c290d0fb1ca068ffaddf22cbd0.pdf" and scan for files "MyApp_*.pdf" and then just parse the filename to get date and hash or CRC value. This way can be done with .NET with no 3rd party libraries required too.
2
On

Set the "Producer" document information property.

2
On

Try something like this:

void Main()
{
    var myfile = "somefile.pdf";
    File.WriteAllBytes(myfile+".myfile","");
}

Then you just look for your "myfiles"

Directory.GetFiles("path","*.myfile")

Basically you are creating a zero byte file that has your original file in it's name. Once you have all the "*.myfile" files back.. you can get to your original by striping the ".myfile" from the name and deleting the 'myfile' version when you are done processing it.