无论移动,重命名和内容修改,是否有办法在文件的生命周期内唯一标识文件(可能还有目录)?(Windows 2000及更高版本).制作文件的副本应该为副本提供它自己的唯一标识符.
我的应用程序将各种元数据与单个文件相关联.如果修改,重命名或移动文件,则能够自动检测和更新文件关联将非常有用.
FileSystemWatcher可以提供通知这些更改的事件,但是如果快速发生许多文件系统事件,它会使用可以轻松填充(和事件丢失)的内存缓冲区.
哈希是没有用的,因为文件的内容可以改变,因此哈希值会改变.
我曾想过使用文件创建日期,但是有一些情况下这不是唯一的(即复制多个文件时).
我还听说过NTFS中的文件SID(安全ID?),但我不确定这是否能满足我的要求.
有任何想法吗?
我使用下面的代码来获取物理磁盘大小,但返回的大小不正确.我用其他工具检查了尺寸.
以下代码报告
总磁盘空间:8.249.955.840字节
它应该是
总磁盘空间:8.254.390.272字节
如何检索实际/正确的物理磁盘大小?在USB驱动器和普通硬盘上测试.代码很长,这里将它分开来显示.
结构:
[StructLayout(LayoutKind.Sequential)]
internal struct DiskGeometry {
public long Cylinders;
public int MediaType;
public int TracksPerCylinder;
public int SectorsPerTrack;
public int BytesPerSector;
}
Run Code Online (Sandbox Code Playgroud)
原生方法:
internal static class NativeMethods {
[DllImport("Kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern SafeFileHandle CreateFile(
string fileName,
uint fileAccess,
uint fileShare,
IntPtr securityAttributes,
uint creationDisposition,
uint flags,
IntPtr template
);
[DllImport("Kernel32.dll", SetLastError=false, CharSet=CharSet.Auto)]
public static extern int DeviceIoControl(
SafeFileHandle device,
uint controlCode,
IntPtr inBuffer,
uint inBufferSize,
IntPtr outBuffer, …Run Code Online (Sandbox Code Playgroud)