Katy
Visual C++ Stuff
Delete file after reboot
A method of deleting a file the next time you reboot your computer. Naturally, it's different depending on whether you're using Windows NT (easy) or Windows 95 (pain in the neck).
// What OS are we using? Use proper deletion method.
OSVERSIONINFO osi;
osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
VERIFY(::GetVersionEx(&osi));
switch(osi.dwPlatformId)
{
case VER_PLATFORM_WIN32_WINDOWS:
{
// Build the deletion instructions
char szAction[128];
lstrcpy(szAction, "NUL=C:\\FILE\\TO\\DELETE.TXT");
// add the action to [rename] section of the WININIT.INI file
const int BUFSIZE=32768; // max size + 1, i'm hedging my bets
char RenameSection[BUFSIZE];
char szWinInitFile[_MAX_PATH];
GetWindowsDirectory(szWinInitFile, _MAX_PATH);
Note: the lstrcat in the next line does the wrong thing if the windows directory happens to be a root directory -- you'll get C:\\WININIT.INI instead of C:\WININIT.INI, and the GetPrivateProfileSection won't work.
lstrcat(szWinInitFile, "\\WININIT.INI");
The idea to use GetPrivateProfileSection came from a post by 'homer@cqgrd.com', found in http://www.dejanews.com WritePrivateProfileString is no good since you could be deleting multiple files, see KB Q140570
int length = GetPrivateProfileSection("Rename", RenameSection,
BUFSIZE, szWinInitFile);
lstrcpy(RenameSection + length, szAction);
*(RenameSection + length + lstrlen(szAction) + 1) = '\0';
WritePrivateProfileSection("rename", RenameSection, szWinInitFile);
break;
}
case VER_PLATFORM_WIN32_NT:
{
// Call MoveFileEx to delete after reboot
MoveFileEx("C:\\FILE\\TO\\DELETE.TXT", NULL,
MOVEFILE_DELAY_UNTIL_REBOOT);
break;
}
default:
// Unknown operating system.
ASSERT(0);
} //switch
Other ways to accomplish the same thing can be found in the January 1996 Microsoft Systems Journal in the Win32 Q&A column.
For people with an MSDN CD who are viewing this with Internet Explorer, you should be able to jump to all these references on your CD. (Other browsers will complain about badly formed URLs). If Microsoft was smart enough not to change these bookmarks between MSDN issues, this should work for everyone.
No solicitation beyond this point.
Don't even think of adding me to your mailing list. Put up a web page I'm
interested in, and let me ask you for information.
Comments? Questions? Send mail to Katy
This page was last modified on 1998/08/23