2

I'm trying to save pictures in a folder and every hour I want to save new pictures with the same name as the old ones. I've tried deleting the old pics, and when debugging, they get deleted but when I try to create new versions with the same name, the picture reappears with the old date and time.

This is my code:

 public void SaveThumbnailsToFolder(List<Thumbnail> thumbnails, Profile p)      {

            foreach (Thumbnail thumbnail in thumbnails)
            {
                Bitmap image = new Bitmap(thumbnail.Image);
                try
                {
                    string path = Path.Combine(p.ThumbnailDownloadFileLocation, String.Format(thumbnail.Name + ".jpg"));
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    image.Save(path);
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
            }
        }

Any ideas of what I'm doing wrong?

jdweng
  • 33,250
  • 2
  • 15
  • 20
ChS
  • 23
  • 5
  • Does that mean: a) No errors in the log and b) It works ok when debugging? – TaW Jul 06 '20 at 08:28
  • 1
    Make sure the path is correct. If the file is getting deleted than the time would be different. I do not think the file is actually being deleted. – jdweng Jul 06 '20 at 08:28
  • Basically, `image.Save()` overwrite the file. I cannot see any problem. –  Jul 06 '20 at 08:32
  • But it's not overwriting since the time is still the old time. – ChS Jul 06 '20 at 08:36
  • No errors and the file is dissapearing from the folder on File.Delete and then comes back with the old time when image.Save – ChS Jul 06 '20 at 08:38
  • Maybe you write out the same image again? you can test by changing the image maybe like so: `using (Graphics g = Graphics.FromImage(image)) g.FillEllipse(Brushes.Green, 5, 5, 55, 55);` But of course it shouldn't have the old time! – TaW Jul 06 '20 at 09:20

3 Answers3

1

If the file is actually deleted but reappear with the same date, then my guess is that the operating system sets the date using meta data inside the image file. You could try changing the modified date your self

File.SetLastWriteTime(path, DateTime.Now);

Look at the below image showing the properties dialog for a file on Windows, the modified date is before the creation date.

File modified date is earlier than creation date

Stekeblad
  • 49
  • 4
1

but when I try to create new versions with the same name, the picture reappears with the old date and time.

For this problem, you can use SetCreationTimeUtc after saving the image to ensure that the image creation time is the current time.

 image.Save(path);
 File.SetCreationTimeUtc(path, DateTime.UtcNow);

Here is my test result:

enter image description here

LouraQ
  • 6,443
  • 2
  • 6
  • 16
0

Assuming the question is about file creation timestamps, this is an old and documented behavior of Windows. From the Remarks for the GetFileTime function:

If you rename or delete a file, then restore it shortly thereafter, Windows searches the cache for file information to restore. Cached information includes its short/long name pair and creation time.


[ EDIT ]   More background in Raymond Chen's The apocryphal history of file system tunnelling:

Why does tunneling exist at all?

When you use a program to edit an existing file, then save it, you expect the original creation timestamp to be preserved, since you’re editing a file, not creating a new one. But internally, many programs save a file by performing a combination of save, delete, and rename operations (such as the ones listed in the linked article), and without tunneling, the creation time of the file would seem to change even though from the end user’s point of view, no file got created.

dxiv
  • 16,984
  • 2
  • 27
  • 49