Windows Directory Management 3

 

 

 

 

 

Using Directory Management Functions

 

The following program examples demonstrate the use of the directory management functions through program examples:

 

  1. Changing the Current Directory
  2. Listing the Files in a Directory
  3. Moving Directories
  4. Obtaining Directory Change Notifications
  5. and etc.

 

Changing the Current Directory Program Example

 

The directory at the end of the active path is called the current directory; it is the directory in which the active application started, unless explicitly changed. An application can determine which directory is current by calling the GetCurrentDirectory() function. It is sometimes necessary to use the GetFullPathName() function to ensure the drive letter is included if the application requires it. Although each process can have only one current directory, if the application switches volumes using the SetCurrentDirectory() function, the system remembers the last current path for each volume (drive letter). This behavior will manifest itself only when specifying a drive letter without a fully qualified path when changing the current directory point of reference to a different volume. This applies to either Get() or Set() operations. An application can change the current directory by calling the SetCurrentDirectory() function. The following example demonstrates the use of GetCurrentDirectory() and SetCurrentDirectory().

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

                              

Changing the Current Directory Program Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

#define BUFSIZE MAX_PATH

 

int wmain(int argc, WCHAR **argv)

{

   WCHAR Buffer[BUFSIZE];

   DWORD dwRet;

 

   if(argc != 2)

   {

         wprintf(L"Changing directory and back to the original path\n");

         wprintf(L"Usage: %s <dir>\n", argv[0]);

         wprintf(L"Example: %s D:\\test\n", argv[0]);

         return 1;

   }

 

   dwRet = GetCurrentDirectory(BUFSIZE, Buffer);

 

   if(dwRet == 0)

   {

      wprintf(L"GetCurrentDirectory() failed, error %d\n", GetLastError());

      return 1;

   }

 

   if(dwRet > BUFSIZE)

   {

      wprintf(L"Buffer too small; need %d characters\n", dwRet);

      return 1;

   }

 

   if(!SetCurrentDirectory(argv[1]))

   {

      wprintf(L"SetCurrentDirectory() failed, error %d\n", GetLastError());

      return 1;

   }

 

   wprintf(L"Set current directory to %s\n", argv[1]);

   // Verify

   system("dir");

 

   if(!SetCurrentDirectory(Buffer))

   {

      wprintf(L"SetCurrentDirectory() failed, error %d\n", GetLastError());

      return 1;

   }

 

   wprintf(L"\nBack to the previous directory,\n\n %s\n\n", Buffer);

   // Verify

   system("dir");

 

   return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

Changing the Current Directory Program Example: A sample console program output in action

 

 

 

 

< Windows Directory 2 | Win32 Programming | Win32 Directory Index | Windows Directory 4 >