views:

34

answers:

2

What is the easiest way in Windows to get the location of a file?

I have a filename that was returned to me by the Windows function GetModuleName (returns the full name of a module (exe or dll)), and which could be in any valid filename format, e.g.

  • myfile.dll
  • c:\windows\myfile.dll
  • \?\c:\windows\myfile.dll (or something like this)
  • x:\bin\myfile.dll
  • \myserver\myshare\myfile.dll

What is the easiest way to know whether the path refers to a local drive or to a network drive?

A: 

This code might help you, it determines whether a folder is on a network share or not.

#include <windows.h>
#include <stdio.h>
#include <lm.h>

int main()
{
   PSHARE_INFO_502 BufPtr, p;
   NET_API_STATUS res;
   LPTSTR   lpszServer = NULL;
   DWORD er = 0,tr = 0,resume = 0, i;

   do
   {
      res = NetShareEnum(lpszServer, 502, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);

      if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
      {
         p = BufPtr;

         for(i = 1; i <= er; i++)
         {
             printf("%S\t\t%S\n", p->shi502_netname, p->shi502_path);
             p++;
         }

         NetApiBufferFree(BufPtr);
      }
      else 
         printf("Error: %ld\n", res);
   }
   while(res == ERROR_MORE_DATA);

   system("pause");
   return 0;
}
Tony
+1  A: 

GetFullPathName() will help normalize the path name. I don't think you need it though. You'd want to go through the handle. So call CreateFile(), get a handle, then call e.g. GetFinalPathNameByHandle(VOLUME_NAME_GUID) This works because network drives don't have volume GUIDs.

MSalters