-1

I'm writing a program that changes the modification date of all files in a directory.

I can apparantly change the properties, but not for windows to recognize it.

private static void ChangeDateTypeOfFile(DateTime date, string filepath)
{
    int state = PdfReader.TestPdfFile(filepath);
    if (state != 0)
    {
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        PdfDocument doc = PdfReader.Open(filepath);
        doc.Info.ModificationDate = date;
        doc.Save(filepath);
    }
    else
    {
        Console.WriteLine(state);
    }
}

When I read the property before and after I change it I can see the difference and it seems to work. Yet in Windows File Explorer I can't see any changes (nor with title and author for example). And the last modified date changes to when I ran the code.
How can I persistently change this information?

Edit:
When I open it with Notepad I can see the line:

/ModDate(D:20070129000000+01'00') //Set the date to 2007

Why does the file Explorer not display this information?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Peter
  • 1,844
  • 2
  • 31
  • 55
  • 2
    I don't know of PDFSharp but have you tried the System.IO call : [File.SetLastWriteTime()](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.setlastwritetime?view=netframework-4.7.2) ? – Antry Mar 20 '19 at 10:44
  • Just found it on google, simpler and cleaner. – Peter Mar 20 '19 at 10:47

2 Answers2

3

Why does the file Explorer not display this information?

Because your code is setting the PDF's internal modification date, that is stored as metadata inside that PDF. This date intentionally has nothing to do with Windows' file timestamps, as these can easily be set to arbitrary values by all sorts of tools, whereas the PDF modification date can only be set by a PDF editor tool.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
-1

Thanks to Antry's comment:

private static void ChangeDate(DateTime date, string filepath)
{
    File.SetLastWriteTime(filepath, date);
}

PdfSharp is an overkill for this task.

Peter
  • 1,844
  • 2
  • 31
  • 55
  • You might be in need of both, since I do not think this changes the PDF's internal modification date, which could cause to have incoherent modified dates between the system and the file. I have low context of your project, so this will be your call. – Antry Mar 20 '19 at 13:46
  • Since your function is `ChangeDateTypeOfFile(...)` you probably need to filter the extensions of files and delegate any modifications to a other method which handles the modification for the appropriate file type; if you're going to modify different types of files. So that you handle any internal or external date modifications. – Antry Mar 20 '19 at 13:49