How can I get the UNC path for a file that is accessed via a network drive?

I am working on an application in VC ++ where network drives are used to access files. Drives are manually assigned by users, and then select a drive in the application. This leads to the fact that disks do not always map to the same servers.

How can I get the UNC path to such a file? This is mainly for identification purposes.

+6
source share
2 answers

here is the function I use to convert the normal path to a 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;; } 
+6
source

Suggest using WNetGetConnection.

The API description is here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385453(v=vs.85).aspx

+1
source

Source: https://habr.com/ru/post/908175/


All Articles