How to access hidden partitions/volumes

心不动则不痛 提交于 2019-12-04 16:33:25

The FindFirstVolume() API returns a path to the root of each volume on the system.

For example, this code prints the path to the first volume, and the name of the first file in the root directory of that volume:

    HANDLE h1, h2;
    wchar_t volpath[4096];
    WIN32_FIND_DATA find_data;

    h1 = FindFirstVolume(volpath, _countof(volpath));

    printf("%ws\n", volpath);

    wcscat_s(volpath, _countof(volpath), L"*.*");

    h2 = FindFirstFile(volpath, &find_data);

    printf("%ws\n", find_data.cFileName);

(In production code, you would need to add error checking, etc.)

Addendum

FindFirstVolume returns a path like this: \\?\Volume{6ff7748e-78db-4838-8896-254b074918f5}\

If you're using the Win32 API (CreateFile, etc.) in C++ you can use that path directly, but due to a bug or limitation in .NET it doesn't work with file management classes such as Directory.GetFiles(). (You could P/Invoke to the Win32 API, of course, but that's awkward.)

Instead, you can work around the problem by replacing the question mark that appears at the beginning of the path with a dot:

var files = Directory.GetFiles(@"\\.\Volume{6ff7748e-78db-4838-8896-254b074918f5}\");

If your program is Windows specific you can work with WMI. I worked a lot with WMI and it is very Handy to do any Kind of Manipulation or getting data about Windows Systems.

First of all you can download the wmiexplorer to see the available data and classes. the relevant Namespace for you will be root\cimv. There you can find various interesting classes for you:

  • Win32_LogicalDisk: Contains all disks, also hidden ones.
  • CIM_Directory: Contains all directories (As far as I know also from hidden Disks)
  • CIM_DataFile: Contains all files (I also think this one contains also the files from hidden disks)
  • There are many more classes you can use e.g. to retrieve file permissions... Microsoft has a pretty good documentation about it

Next, you can implement your desired WMI queries into your C# (or VB.NET) program. For example, this one will print all disks:

WqlObjectQuery wqlQuery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wqlQuery);

foreach (ManagementObject disk in searcher.Get()) 
{
    Console.WriteLine(disk.ToString());
}

As you can seen, WMI supports queries like SQL (although it's not as powerful...)

WMI also supports methots for most of ist classes. For example you can check each disk for Errors:

foreach (ManagementObject disk in searcher.Get()) 
{
    if(shouldCheckThisDisk)
        disk.Chkdsk();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!