问题描述
我正在用C#编写一个函数,该函数返回卷的安装点.
I'm writing a function in C# which returns mount points for a volume.
[
DllImport ( "kernel32.dll" , CharSet = CharSet .Unicode,SetLastError = true )]
DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError =true)]
私有 静态 外部 IntPtr FindFirstVolumeMountPointW( 字符串 lpszRootPathName, StringBuilder lpszVolumeMountPoint, Int32 cchBufferLength);
privatestaticexternIntPtr FindFirstVolumeMountPointW(string lpszRootPathName, StringBuilder lpszVolumeMountPoint,Int32 cchBufferLength);
公共 静态 列表 < 字符串 > GetMountPoints( 字符串 guidVolumePath)
publicstaticList<string> GetMountPoints(string guidVolumePath)
{
列表 < 字符串 > allMountPoints = 新 列表 < 字符串 >();
List<string> allMountPoints = newList<string>();
StringBuilder outVolumeMountPoint = 新 StringBuilder (50);
StringBuilder outVolumeMountPoint =newStringBuilder(50);
IntPtr _searchHandle = FindFirstVolumeMountPointW(guidVolumePath,outVolumeMountPoint,outVolumeMountPoint.Capacity);
IntPtr _searchHandle = FindFirstVolumeMountPointW(guidVolumePath, outVolumeMountPoint, outVolumeMountPoint.Capacity);
int errorRes = 元帅 .GetLastWin32Error();
int errorRes =Marshal.GetLastWin32Error();
开关 (errorRes)
switch (errorRes)
{
案例 ERROR_ACCESS_DENIED:
case ERROR_ACCESS_DENIED:
抛出 新 Win32Exception ( 访问权限 ". );
thrownewWin32Exception("Access is denied.");
案例 ERROR_PATH_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
抛出 新 Win32Exception ( 路径 ).
thrownewWin32Exception("Path not found.");
}
如果 (errorRes == ERROR_NO_MORE_FILES)
if (errorRes == ERROR_NO_MORE_FILES)
{
返回 allMountPoints;
return allMountPoints;
}
其他 如果 (errorRes!= ERROR_SUCCESS)
elseif (errorRes != ERROR_SUCCESS)
{
抛出 新 Win32Exception (errorRes);
thrownewWin32Exception(errorRes);
}
allMountPoints.Add(outVolumeMountPoint.ToString());
allMountPoints.Add(outVolumeMountPoint.ToString());
int res = 0;
int res = 0;
如果 ((res = FindVolumeMountPointClose(_searchHandle))== 0)
if ((res = FindVolumeMountPointClose(_searchHandle)) == 0)
{
errorRes =
errorRes =
元帅 .GetLastWin32Error();
Marshal.GetLastWin32Error();
抛出 新 Win32Exception (errorRes);
thrownewWin32Exception(errorRes);
}
返回 allMountPoints;
return allMountPoints;
}
无论我作为guidVolumePath传递什么卷guid,我总是以_searchHandle的形式获得-1,并且GetLastWin32Error返回ERROR_NO_MORE_FILES,就好像该卷没有安装点.但我通过像 \\?\ Volume {3652c18d-ac28-11de-bb9f-806e6f6e6963} \ ;这是Windows 7计算机上的C:\驱动器.
No matter what volume guid I pass as guidVolumePath I always get -1 as _searchHandle and the GetLastWin32Error returns ERROR_NO_MORE_FILES as if the volume has no mount points. But I pass a volume like\\?\Volume{3652c18d-ac28-11de-bb9f-806e6f6e6963}\ which is a C:\ drive on my Windows 7 computer.
我在做什么错了?
Dinko
推荐答案
以下伪代码可以帮助您弄清楚如何获取所需的内容(当然,您需要添加错误检查/处理等):
The following pseudocode may help you figure out how to get what you're looking for (of course, you need to add error checking/handling, etc) :
TCHAR szVolumeName [MAX_PATH];
FindFirstVolume(szVolumeName);
做
{
TCHAR szPathNames [MAX_PATH * 5];
TCHAR szMountPoint [MAX_PATH];
TCHAR szMountedVolume [MAX_PATH];
TCHAR szVolumeName[MAX_PATH];
FindFirstVolume( szVolumeName );
do
{
TCHAR szPathNames[MAX_PATH*5];
TCHAR szMountPoint[MAX_PATH];
TCHAR szMountedVolume[MAX_PATH];
//
//打印名为szVolumeName的卷安装在的路径...
//
if(GetVolumePathNamesForVolumeName(szVolumeName,szPathNames,_countof(szPathNames))
&& szPathNames [0])
{
for(LPCTSTR psz = szPathNames; * psz; psz + =(_tcslen(psz)+1))
{
_tprintf(_T(安装在\%s \" \ r \ n处的_T(" volume \%s \"),szVolumeName,psz);
}
}
其他
{
_tprintf(_T("volume \"%s \未安装在任何地方\ r \ n"),szVolumeName);
}
//
// print the paths that the volume named szVolumeName is mounted at...
//
if( GetVolumePathNamesForVolumeName(szVolumeName,szPathNames,_countof(szPathNames))
&& szPathNames[0] )
{
for( LPCTSTR psz = szPathNames; *psz ; psz += (_tcslen(psz)+1) )
{
_tprintf( _T("volume \"%s\" mounted at \"%s\"\r\n"), szVolumeName, psz );
}
}
else
{
_tprintf( _T("volume \"%s\" is not mounted anywhere\r\n"), szVolumeName );
}
//
//在名为szVolumeName的卷上找到所有装有其他卷的子目录
//
FindFirstVolumeMountPoint(szVolumeName,szMountPoint,_countof(szMountPoint));
做
{
//
//如果该卷已安装在任何位置,则使用该卷的第一个路径作为该卷上安装点的基本路径
//
if(szPathNames [0])
{
PathCombine(szMountPoint,szPathNames,szMountPoint);
}
//
//获取当前在此挂载点挂载的卷的名称.
//
GetVolumeNameForVolumeMountPoint(szMountPoint,szMountedVolume,_countof(szMountedVolume));
//
//并打印出来...
//
_tprintf(_T(文件夹\"%s \具有安装在其上的卷\"%s \"\ r \ n"),szMountPoint,szMountedVolume);
}
while(FindNextVolumeMountPoint(szMountPoint));
} while(FindNextVolume(szVolumeName));
//
// find all subdirectories on the volume named szVolumeName that have OTHER volumes mounted there
//
FindFirstVolumeMountPoint( szVolumeName, szMountPoint, _countof(szMountPoint) );
do
{
//
// use the first path for the volume, if it's mounted anywhere, as the base path for the mount points on it
//
if( szPathNames[0] )
{
PathCombine( szMountPoint, szPathNames, szMountPoint );
}
//
// get the name of the volume that is currently mounted at this mount point.
//
GetVolumeNameForVolumeMountPoint( szMountPoint, szMountedVolume, _countof(szMountedVolume) );
//
// and print it...
//
_tprintf( _T("folder \"%s\" has volume \"%s\" mounted on it\r\n"), szMountPoint, szMountedVolume );
}
while( FindNextVolumeMountPoint(szMountPoint) );
} while( FindNextVolume(szVolumeName) );
这篇关于FindFirstVolumeMountPoint始终返回ERROR_NO_MORE_FILES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!