给定一个绝对文件系统路径,我如何检测它是否在NTFS分区上?我更喜欢C#中的帮助,但Win32 / C会这样做。该软件将运行的系统是Windows Vista或更高版本。
答案 0 :(得分:2)
//Get all the drives on the local machine.
DriveInfo[] allDrives = DriveInfo.GetDrives();
//Get the path root.
var pathRoot = Path.GetPathRoot(absoluteFilePath);
//Find the drive based on the path root.
var driveBasedOnPath = allDrives.FirstOrDefault(d => d.RootDirectory.Name == pathRoot);
//Determine if NTFS
var isNTFS = driveBasedOnPath != null ? driveBasedOnPath.DriveFormat == "NTFS" : false;
答案 1 :(得分:2)
您可以使用FSCTL_FILESYSTEM_GET_STATISTICS
来确定文件系统类型。
这是一些示例代码。我已经检查过它是否正确处理了挂载点,即它检测到目标卷的类型,而不是源卷。您不需要指定挂载点本身(尽管可以),但您指定的文件或目录必须存在。
#define _WIN32_WINNT 0x0502
#include <windows.h>
#include <stdio.h>
int wmain(int argc, wchar_t ** argv)
{
HANDLE h;
FILESYSTEM_STATISTICS * fs;
BYTE buffer[32768];
DWORD dw;
h = CreateFile(argv[1], 0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h == INVALID_HANDLE_VALUE)
{
printf("CreateFile: %u\n", GetLastError());
return 1;
}
if (!DeviceIoControl(h, FSCTL_FILESYSTEM_GET_STATISTICS, NULL, 0, buffer, sizeof(buffer), &dw, NULL))
{
dw = GetLastError();
CloseHandle(h);
printf("DeviceIoControl: %u\n", dw);
if (dw == ERROR_INVALID_FUNCTION)
{
printf("This probably means the specified file or directory is not on an NTFS volume.\n");
printf("For example, this happens if you specify a file on a CD-ROM.\n");
}
return 1;
}
CloseHandle(h);
fs = (FILESYSTEM_STATISTICS *)buffer;
printf("Filesystem type: %u\n", fs->FileSystemType);
if (fs->FileSystemType == FILESYSTEM_STATISTICS_TYPE_NTFS)
{
printf("The file or directory is on an NTFS volume.\n");
}
else
{
printf("The file or directory is not on an NTFS volume.\n");
}
return 0;
}
答案 2 :(得分:0)
您想要查看DriveInfo
课程。类似的东西:
var drive = DriveInfo.GetDrives().SingleOrDefault(di => di.Name.StartsWith("C"));
Console.WriteLine("C drive: {0}", drive.DriveFormat);
drive.DriveFormat
将输出格式,因此您可以检查它是否为NTFS。
答案 3 :(得分:0)
using System.Management;
string logDisk= "c:";
string CIMObject = String.Format("win32_LogicalDisk.DeviceId='{0}'", logDisk);
using(ManagementObject mo = new ManagementObject(CIMObject))
{
mo.Get();
Console.WriteLine(mo["FileSystem"]);
}
答案 4 :(得分:0)
Win32 GetVolumeInformation() API将为您提供给定根目录路径的文件系统的名称。
请注意,这将遵循符号链接/连接,并返回此类链接目标的信息。