.Net 库在保留时间戳的同时移动/复制文件

发布于 2024-12-01 07:20:03 字数 112 浏览 5 评论 0 原文

有谁知道 .Net 库可以在不更改任何时间戳的情况下复制/粘贴或移动文件。我正在寻找的功能包含在一个名为 robocopy.exe 的程序中,但我希望无需共享该二进制文件即可使用此功能。

想法?

Does anyone know of a .Net library where a file can be copied / pasted or moved without changing any of the timestamps. The functionality I am looking for is contained in a program called robocopy.exe, but I would like this functionality without having to share that binary.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

老街孤人 2024-12-08 07:20:03
public static void CopyFileExactly(string copyFromPath, string copyToPath)
{
    var origin = new FileInfo(copyFromPath);

    origin.CopyTo(copyToPath, true);

    var destination = new FileInfo(copyToPath);
    destination.CreationTime = origin.CreationTime;
    destination.LastWriteTime = origin.LastWriteTime;
    destination.LastAccessTime = origin.LastAccessTime;
}
public static void CopyFileExactly(string copyFromPath, string copyToPath)
{
    var origin = new FileInfo(copyFromPath);

    origin.CopyTo(copyToPath, true);

    var destination = new FileInfo(copyToPath);
    destination.CreationTime = origin.CreationTime;
    destination.LastWriteTime = origin.LastWriteTime;
    destination.LastAccessTime = origin.LastAccessTime;
}
流绪微梦 2024-12-08 07:20:03

当没有管理权限执行时,Roy 的答案将在尝试覆盖现有只读文件或尝试在复制的只读文件上设置时间戳时抛出异常(UnauthorizedAccessException)。

以下解决方案基于 Roy 的答案,但将其扩展为覆盖只读文件并更改复制的只读文件的时间戳,同时保留文件的只读属性,同时仍然在没有管理员权限的情况下执行。

public static void CopyFileExactly(string copyFromPath, string copyToPath)
{
    if (File.Exists(copyToPath))
    {
        var target = new FileInfo(copyToPath);
        if (target.IsReadOnly)
            target.IsReadOnly = false;
    }

    var origin = new FileInfo(copyFromPath);
    origin.CopyTo(copyToPath, true);

    var destination = new FileInfo(copyToPath);
    if (destination.IsReadOnly)
    {
        destination.IsReadOnly = false;
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
        destination.IsReadOnly = true;
    }
    else
    {
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
    }
}

When executing without administrative privileges Roy's answer will throw an exception (UnauthorizedAccessException) when attempting to overwrite existing read only files or when attempting to set the timestamps on copied read only files.

The following solution is based on Roy's answer but extends it to overwrite read only files and to change the timestamps on copied read only files while preserving the read only attribute of the file all while still executing without admin privilege.

public static void CopyFileExactly(string copyFromPath, string copyToPath)
{
    if (File.Exists(copyToPath))
    {
        var target = new FileInfo(copyToPath);
        if (target.IsReadOnly)
            target.IsReadOnly = false;
    }

    var origin = new FileInfo(copyFromPath);
    origin.CopyTo(copyToPath, true);

    var destination = new FileInfo(copyToPath);
    if (destination.IsReadOnly)
    {
        destination.IsReadOnly = false;
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
        destination.IsReadOnly = true;
    }
    else
    {
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
    }
}
野却迷人 2024-12-08 07:20:03

You can read and write all the timestamps there are, using the FileInfo class:

帅气称霸 2024-12-08 07:20:03

您应该能够读取所需的值,进行所需的任何更改,然后使用 文件信息

You should be able to read the values you need, make whatever changes you wish and then restore the previous values by using the properties of FileInfo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文