在 Compact Framework 中更改文件 LastWriteDate

发布于 2024-11-01 11:12:19 字数 77 浏览 1 评论 0原文

FileSystemInfo.LastWriteTime 属性在 CF 中是只读的。 有其他方法可以更改该日期吗?

FileSystemInfo.LastWriteTime property is readonly in CF.
Is there an alternative way to change that date?

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

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

发布评论

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

评论(2

往昔成烟 2024-11-08 11:12:19

P/Invoke SetFileTime

编辑

类似的东西(警告:未经测试)

[DllImport("coredll.dll")]
private static extern bool SetFileTime(string path,
                                      ref long creationTime,
                                      ref long lastAccessTime,
                                      ref long lastWriteTime);

public void SetFileTimes(string path, DateTime time)
{
    var ft = time.ToFileTime();
    SetFileTime(path, ref ft, ref ft, ref ft);
}

P/Invoke SetFileTime.

EDIT

Something along these lines (warning: untested)

[DllImport("coredll.dll")]
private static extern bool SetFileTime(string path,
                                      ref long creationTime,
                                      ref long lastAccessTime,
                                      ref long lastWriteTime);

public void SetFileTimes(string path, DateTime time)
{
    var ft = time.ToFileTime();
    SetFileTime(path, ref ft, ref ft, ref ft);
}
叹梦 2024-11-08 11:12:19

这是一个更完整的实现,改编自上面提供的答案 ctacke这个 StackOverflow 问题。我希望这对某人有用:

// Some Windows constants
// File access (using CreateFileW)
public const uint GENERIC_READ          = 0x80000000;
public const uint GENERIC_WRITE         = 0x40000000;
public const uint GENERIC_READ_WRITE    = (GENERIC_READ + GENERIC_WRITE);
public const int INVALID_HANDLE_VALUE   = -1;

// File creation (using CreateFileW)
public const int CREATE_NEW             = 1;
public const int OPEN_EXISTING          = 3;

// File attributes (using CreateFileW)
public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;

// P/Invokes
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr CreateFileW(
    string lpFileName,
    uint dwDesiredAccess,
    uint dwShareMode,
    IntPtr pSecurityAttributes,
    uint dwCreationDisposition,
    uint dwFlagsAndAttributes,
    IntPtr hTemplatefile);

[DllImport("coredll.dll", SetLastError = true)]
public static extern int CloseHandle(IntPtr hObject);

// Note: Create related P/Invokes to change creation or last access time.
// This one modifies the last write time only.
[DllImport("coredll.dll", EntryPoint = "SetFileTime", SetLastError = true)]
private static extern bool SetFileWriteTime(
    IntPtr hFile,
    IntPtr lpCreationTimeUnused,
    IntPtr lpLastAccessTimeUnused,
    ref long lpLastWriteTime);

// Open a handle to the file you want changed
IntPtr hFile = CreateFileW(
    path, GENERIC_READ_WRITE, 0,
    IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
    IntPtr.Zero);

// Modify the last write time and close the file
long lTimeNow = DateTime.Now.ToFileTime();
SetFileWriteTime(hFile, IntPtr.Zero, IntPtr.Zero, ref lTimeNow);
CloseHandle(hFile);

请注意,如果需要,您可以使用 System.IO.File.GetLastWriteTime(在 .NET Compact Framework 中公开)来读取上次写入时间。

Here is a fuller implementation, adapted from the answer ctacke provides above and this StackOverflow question. I hope this proves useful to someone:

// Some Windows constants
// File access (using CreateFileW)
public const uint GENERIC_READ          = 0x80000000;
public const uint GENERIC_WRITE         = 0x40000000;
public const uint GENERIC_READ_WRITE    = (GENERIC_READ + GENERIC_WRITE);
public const int INVALID_HANDLE_VALUE   = -1;

// File creation (using CreateFileW)
public const int CREATE_NEW             = 1;
public const int OPEN_EXISTING          = 3;

// File attributes (using CreateFileW)
public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;

// P/Invokes
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr CreateFileW(
    string lpFileName,
    uint dwDesiredAccess,
    uint dwShareMode,
    IntPtr pSecurityAttributes,
    uint dwCreationDisposition,
    uint dwFlagsAndAttributes,
    IntPtr hTemplatefile);

[DllImport("coredll.dll", SetLastError = true)]
public static extern int CloseHandle(IntPtr hObject);

// Note: Create related P/Invokes to change creation or last access time.
// This one modifies the last write time only.
[DllImport("coredll.dll", EntryPoint = "SetFileTime", SetLastError = true)]
private static extern bool SetFileWriteTime(
    IntPtr hFile,
    IntPtr lpCreationTimeUnused,
    IntPtr lpLastAccessTimeUnused,
    ref long lpLastWriteTime);

// Open a handle to the file you want changed
IntPtr hFile = CreateFileW(
    path, GENERIC_READ_WRITE, 0,
    IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
    IntPtr.Zero);

// Modify the last write time and close the file
long lTimeNow = DateTime.Now.ToFileTime();
SetFileWriteTime(hFile, IntPtr.Zero, IntPtr.Zero, ref lTimeNow);
CloseHandle(hFile);

Note that you can use System.IO.File.GetLastWriteTime (which is exposed in the .NET Compact Framework) to read the last write time if required.

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