GetCurrentDirectory(), SetCurrentDirectory(), GetWindowsDirectory() GetSystemDirectory()

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Getting the Windows and system directory

To show: Using GetCurrentDirectory(), SetCurrentDirectory(), GetWindowsDirectory() GetSystemDirectory() functions

 

 

#include <windows.h>

#include <stdio.h>

#define BUFFER_SIZE 200

 

int main(void)

{

TCHAR infoBuf[BUFFER_SIZE];

LPCWSTR lpPathName = L"C:\\Program Files\\Microsoft sql server";

// Get the current working directory

if(!GetCurrentDirectory(BUFFER_SIZE, infoBuf))

printf("GetCurrentDirectory() failed!\n");

printf("Your current directory is: %S\n", infoBuf);

printf("Changing directory...\n");

// Set to current working directory

if(!SetCurrentDirectory(lpPathName))

printf("SetCurrentDirectory() failed!\n");

 

// Do some verification...

if(!GetCurrentDirectory(BUFFER_SIZE, infoBuf))

printf("GetCurrentDirectory() failed!\n");

printf("Your current directory is: %S\n", infoBuf);

 

// Get and display the Windows directory.

if(!GetWindowsDirectory(infoBuf, BUFFER_SIZE))

printf("GetWindowsDirectory() failed!\n");

printf("Your Windows directory is: %S\n", infoBuf);

 

// Get and display the system directory.

if(!GetSystemDirectory(infoBuf, BUFFER_SIZE))

printf("GetSystemDirectory() failed!\n");

printf("Your system directory is: %S\n", infoBuf);

return 0;

}

 

Output example:

 

Your current directory is: f:\vc2005project\cplus\cplus

Changing directory...

Your current directory is: C:\Program Files\Microsoft sql server

Your Windows directory is: C:\WINDOWS

Your system directory is: C:\WINDOWS\system32

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index