删除和创建 new/overwriting 同名文件 c# .NET
deleting and creating new/overwriting file with same name c# .NET
我正在尝试将图片保存在文件夹中,并且每隔一小时我都想保存与旧图片同名的新图片。我试过删除旧照片,在调试时,它们被删除了,但是当我尝试创建具有相同名称的新版本时,图片重新出现并显示旧的日期和时间。
这是我的代码:
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);
}
}
}
你知道我做错了什么吗?
如果文件确实被删除但以相同的日期重新出现,那么我的猜测是操作系统使用图像文件中的元数据设置了日期。 You could try changing the modified date your self
File.SetLastWriteTime(path, DateTime.Now);
查看下图显示 Windows 上文件的属性对话框,修改日期早于创建日期。
File modified date is earlier than creation date
but when I try to create new versions with the same name, the picture
reappears with the old date and time.
对于这个问题,可以在保存图片后使用SetCreationTimeUtc来保证图片创建时间为当前时间。
image.Save(path);
File.SetCreationTimeUtc(path, DateTime.UtcNow);
这是我的测试结果:
假设问题是关于文件 创建 时间戳,这是 Windows 的一个古老的记录行为。来自 Remarks GetFileTime 函数:
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.
[ 编辑 ] 更多背景信息请参见 Raymond Chen 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.
我正在尝试将图片保存在文件夹中,并且每隔一小时我都想保存与旧图片同名的新图片。我试过删除旧照片,在调试时,它们被删除了,但是当我尝试创建具有相同名称的新版本时,图片重新出现并显示旧的日期和时间。
这是我的代码:
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);
}
}
}
你知道我做错了什么吗?
如果文件确实被删除但以相同的日期重新出现,那么我的猜测是操作系统使用图像文件中的元数据设置了日期。 You could try changing the modified date your self
File.SetLastWriteTime(path, DateTime.Now);
查看下图显示 Windows 上文件的属性对话框,修改日期早于创建日期。
File modified date is earlier than creation date
but when I try to create new versions with the same name, the picture reappears with the old date and time.
对于这个问题,可以在保存图片后使用SetCreationTimeUtc来保证图片创建时间为当前时间。
image.Save(path);
File.SetCreationTimeUtc(path, DateTime.UtcNow);
这是我的测试结果:
假设问题是关于文件 创建 时间戳,这是 Windows 的一个古老的记录行为。来自 Remarks GetFileTime 函数:
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.
[ 编辑 ] 更多背景信息请参见 Raymond Chen 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.