Question

I want to delete the folder C:\Users\username\AppData\Roaming\appname when the users uninstall the application appname.

First, I use the following code to get the path C:\Users\username\AppData\Roaming:

TCHAR dir[MAX_PATH];
dir[0] = '\0';
BOOL ok = SHGetSpecialFolderPath(NULL, dir, CSIDL_APPDATA, TRUE);

appname is defined as _T("appname")

The first question is: How to append "appname" to "dir"?

Suppose the above is done. Then I need to use SHFileOperation to delete the non-empty folder C:\Users\username\AppData\Roaming\appname. So I need a double null-terminated string in a SHFILEOPSTRUCT structure. So

How to get a double null-terminated string from the result of the first step? Just append _T("\0\0") to it?

Update: I can use TCHAR *dir2 = lstrcat(dir, appname); to get the path. But when I tried to use TCHAR *dir3 = lstrcat(dir2, _T("\0\0"));, the folder is not deleted. Any number of \0 won't work.


p.s:

If I do the following directly, I got it to work. The problem is that I want to it to be user-independent.

TCHAR path[] = _T("C:\\Users\\username\\AppData\\Roaming\\appname");
memcpy(path + sizeof(path) / sizeof(TCHAR) - 1, _T("\0\0\0"), 3);
Was it helpful?

Solution

For appending paths see PathAppend function.

TCHAR dir[MAX_PATH] = {0};

BOOL ok = SHGetSpecialFolderPath(NULL, dir, CSIDL_APPDATA, TRUE);

PathAppend(dir, _T("appname"));

If you want ensure double null terminating of dir variable:

dir[MAX_PATH - 1] = 0;
dir[MAX_PATH - 2] = 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top