MSDN Documentation

Directory Management

This section provides detailed information on the Windows API functions used for managing directories.

CreateDirectory

Creates a new directory. If the directory already exists, the function succeeds and returns a nonzero value.

Parameters:
  • lpPathName: A pointer to a null-terminated string that specifies the path of the directory to be created.
  • lpSecurityAttributes: A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes.
Return Value:
  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero. To get extended error information, call GetLastError.
Example:

#include <windows.h>
#include <iostream>

int main() {
    if (CreateDirectory(L"C:\\NewDirectory", NULL)) {
        std::wcout << L"Directory created successfully." << std::endl;
    } else {
        if (GetLastError() == ERROR_ALREADY_EXISTS) {
            std::wcout << L"Directory already exists." << std::endl;
        } else {
            std::wcerr << L"Failed to create directory. Error code: " << GetLastError() << std::endl;
        }
    }
    return 0;
}
                    

RemoveDirectory

Marks a directory for deletion. This function marks a directory for removal. The directory is actually removed after the last handle to the directory is closed.

Parameters:
  • lpPathName: A pointer to a null-terminated string that specifies the directory to be removed.
Return Value:
  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero. To get extended error information, call GetLastError.
Example:

#include <windows.h>
#include <iostream>

int main() {
    if (RemoveDirectory(L"C:\\NewDirectory")) {
        std::wcout << L"Directory marked for deletion." << std::endl;
    } else {
        std::wcerr << L"Failed to remove directory. Error code: " << GetLastError() << std::endl;
    }
    return 0;
}
                    

GetCurrentDirectory

Retrieves the current working directory for the specified process.

Parameters:
  • nBufferLength: The size, in characters, of the buffer pointed to by lpBuffer.
  • lpBuffer: A pointer to the buffer that receives a null-terminated string that specifies the current working directory.
Return Value:
  • If the function succeeds and the characters written to lpBuffer, including the null-terminating character, do not exceed nBufferLength, the return value is the number of characters in the string pointed to by lpBuffer, not including the null-terminating character.
  • If lpBuffer is not large enough to hold the directory path, the return value is the required buffer size, in characters, including the null-terminating character.
  • If the function fails, the return value is zero. To get extended error information, call GetLastError.
Example:

#include <windows.h>
#include <iostream>
#include <vector>

int main() {
    DWORD bufferSize = GetCurrentDirectory(0, NULL);
    if (bufferSize > 0) {
        std::vector<wchar_t> buffer(bufferSize);
        if (GetCurrentDirectory(bufferSize, buffer.data()) > 0) {
            std::wcout << L"Current directory: " << buffer.data() << std::endl;
        } else {
            std::wcerr << L"Failed to get current directory. Error code: " << GetLastError() << std::endl;
        }
    } else {
        std::wcerr << L"Failed to get buffer size for current directory. Error code: " << GetLastError() << std::endl;
    }
    return 0;
}
                    

SetCurrentDirectory

Changes the current working directory for the process. The current working directory is the default path that is used when a file is opened if a fully qualified path is not specified.

Parameters:
  • lpPathName: A pointer to a null-terminated string that specifies the new current directory.
Return Value:
  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero. To get extended error information, call GetLastError.
Example:

#include <windows.h>
#include <iostream>

int main() {
    if (SetCurrentDirectory(L"C:\\Windows")) {
        std::wcout << L"Current directory changed successfully." << std::endl;
    } else {
        std::wcerr << L"Failed to change current directory. Error code: " << GetLastError() << std::endl;
    }
    return 0;
}
                    

Related Topics