How can I get a UNC path for a file that is accessed through a network drive?(如何获取通过网络驱动器访问的文件的 UNC 路径?)
问题描述
我正在使用 VC++ 开发一个使用网络驱动器访问文件的应用程序.驱动器由用户手动分配,然后在应用程序中选择驱动器.这会导致驱动器并不总是映射到相同的服务器.
I am working on a application in VC++ where network drives are used to access files. The drives are assigned manually by the users and then select the drive in the application. This results in drives not always being mapped to the same servers.
如何获取此类文件的 UNC 路径?这主要用于识别目的.
How would I go about obtaining the UNC path to such a file? This is mostly for identification purposes.
推荐答案
这是我用来将普通路径转换为 UNC 路径的函数:
here's the function I use to convert a normal path to an UNC path:
wstring ConvertToUNC(wstring sPath)
{
WCHAR temp;
UNIVERSAL_NAME_INFO * puni = NULL;
DWORD bufsize = 0;
wstring sRet = sPath;
//Call WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL option
if (WNetGetUniversalName(sPath.c_str(),
UNIVERSAL_NAME_INFO_LEVEL,
(LPVOID) &temp,
&bufsize) == ERROR_MORE_DATA)
{
// now we have the size required to hold the UNC path
WCHAR * buf = new WCHAR[bufsize+1];
puni = (UNIVERSAL_NAME_INFO *)buf;
if (WNetGetUniversalName(sPath.c_str(),
UNIVERSAL_NAME_INFO_LEVEL,
(LPVOID) puni,
&bufsize) == NO_ERROR)
{
sRet = wstring(puni->lpUniversalName);
}
delete [] buf;
}
return sRet;;
}
这篇关于如何获取通过网络驱动器访问的文件的 UNC 路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取通过网络驱动器访问的文件的 UNC 路径?


- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01