I've searched the web for hours trying to find how to delete a file from the registry but havent found anything.

basically what I want to do is delete a file from a folder inside of the software folder of HKEY_CURRENT_USER for example

HKEY_CURRENT_USER -> Software -> MyProgram -> MySettings -> File

where 'File' is a binary file.

please help, I just want a simple code in C++ with an explanation on how to delete ONE file in the directory above.

Dani AI

Generated

Two different things are commonly described as "delete a file from the registry," so first distinguish the cases: the registry value named File can either contain a path (type REG_SZ / REG_EXPAND_SZ) that points to a file on disk, or it can contain the raw bytes of a file (type REG_BINARY). was right to suggest "read the value then act" — the concrete steps below follow that idea and fill in the missing details and error checks.

Basic safe workflow:

  • Open the key with RegOpenKeyEx (request KEY_READ | KEY_WRITE and, on 64-bit Windows, consider KEY_WOW64_64KEY / KEY_WOW64_32KEY if registry redirection matters).
  • Call RegQueryValueEx to get the value type and size.
  • If the type is REG_SZ/REG_EXPAND_SZ, read the string and remove the disk file (for modern C++ use std::filesystem::remove with std::error_code), then optionally call RegDeleteValue to remove the path from the registry.
  • If the type is REG_BINARY, the stored bytes live in the registry; remove them by calling RegDeleteValue. If the bytes were previously exported to disk, delete that file too.

Example (minimal, illustrative):

#include <windows.h>
#include <filesystem>
#include <string>
#include <system_error>

// open key
HKEY hKey = NULL;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\MyProgram\\MySettings", 0,
                 KEY_READ | KEY_WRITE, &hKey) != ERROR_SUCCESS) { /* handle */ }

DWORD type = 0, cb = 0;
if (RegQueryValueExW(hKey, L"File", NULL, &type, NULL, &cb) == ERROR_SUCCESS) {
    if (type == REG_SZ || type == REG_EXPAND_SZ) {
        std::wstring path;
        path.resize(cb / sizeof(wchar_t));
        RegQueryValueExW(hKey, L"File", NULL, NULL,
                         reinterpret_cast<LPBYTE>(&path[0]), &cb);
        if (!path.empty() && path.back() == L'\0') path.pop_back();
        std::error_code ec;
        std::filesystem::remove(std::filesystem::path(path), ec);
        if (!ec) RegDeleteValueW(hKey, L"File"); // cleanup registry entry
    } else if (type == REG_BINARY) {
        RegDeleteValueW(hKey, L"File"); // remove stored bytes
    }
}
RegCloseKey(hKey);

Cautions and troubleshooting: back up the registry before edits, run with sufficient privileges, check every return code (use GetLastError() on failure), and be aware of 64-bit registry redirection (WOW6432Node) when a 32-bit process accesses HKCU\Software. 's earlier pointer is useful for general Win32 registry details.

Recommended Answers

All 2 Replies

Working with the Registry in C++:

1. Read the value at that registry setting. If you are using non-managed code (normal c++) then you can refer to any of these links for more information.

2. Assuming it is a path to a file, just use standard C function remove() to delete it.

3. You will also want to remove the path and filename from the registry entry to indicate it no longer exists. That prevents leaving orphaned entries in the registry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.