1

给定一个绝对文件系统路径,我如何检测它是否在 NTFS 分区上?我更喜欢 C# 的帮助,但 Win32/C 可以。该软件将在其上运行的系统是 Windows Vista 或更高版本。

4

5 回答 5

3
//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;
于 2012-10-12T21:22:20.643 回答
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;
}
于 2012-10-14T21:23:09.757 回答
1

你想看看DriveInfo课堂。就像是:

var drive = DriveInfo.GetDrives().SingleOrDefault(di => di.Name.StartsWith("C"));
Console.WriteLine("C drive: {0}", drive.DriveFormat);

drive.DriveFormat将输出格式,因此您可以检查它是否为 NTFS。

于 2012-10-12T21:23:26.683 回答
1

Win32 GetVolumeInformation() API 将为您提供给定根目录路径的文件系统名称。

请注意,这将遵循符号链接/连接,并返回此类链接目标的信息。

于 2012-10-14T21:41:04.487 回答
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"]);
}
于 2012-10-12T21:27:53.650 回答