我做了一个EXE文件,我要在里面实现删除EXE所在文件夹的功能,但EXE文件又是运行的,高手,现在该怎么办?最好有源代码。
解决方案 »
- 正则问题
- 为什么C#中“gb2312”不是受支持的编码名
- 变量作用范围的怪事!
- 如何后期动态触发事件
- 关于treeview的单击节点事件
- WPF用BitmapImage加载图片后,如何获取某个坐标点(X,Y)上的ARGB值?
- 我想给程序留个菜单做接口,方便人家做二次开发,具体怎么做,请大家给点思路。
- winform下怎样修改DataGrid的指定单元格内的数据?
- 各位前辈,可以帮我讲讲console.write输出格式控制的东东吗??
- 如何在form中访问main中定义的变量?
- 如何把datareader中得知取出放到label或textbox中,我知道VB的,但不知道C#语法是什么
- 菜鸟问题,如何在自己新写的一个class调用自带class的控件并更改属性
曾经做过一个项目是update本身,然后重启自身,和这个情况类似
Using the codeJust call the Selfdestruct() function before program exit.// this is the name of the temporary .bat file
static const char tempbatname[] = "_uninsep.bat" ;void Selfdestruct()
{
// temporary .bat file
static char templ[] =
":Repeat\r\n"
"del \"%s\"\r\n"
"if exist \"%s\" goto Repeat\r\n"
"rmdir \"%s\"\r\n"
"del \"%s\"" ;
char modulename[_MAX_PATH] ; // absolute path of calling .exe file
char temppath[_MAX_PATH] ; // absolute path of temporary .bat file
char folder[_MAX_PATH] ; GetTempPath(_MAX_PATH, temppath) ;
strcat(temppath, tempbatname) ; GetModuleFileName(NULL, modulename, MAX_PATH) ;
strcpy (folder, modulename) ;
char *pb = strrchr(folder, '\\');
if (pb != NULL)
*pb = 0 ; HANDLE hf ;
hf = CreateFile(temppath, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hf != INVALID_HANDLE_VALUE)
{
DWORD len ;
char *bat ; bat = (char*)alloca(strlen(templ) +
strlen(modulename) * 2 + strlen(temppath) + 20) ; wsprintf(bat, templ, modulename, modulename, folder, temppath) ; WriteFile(hf, bat, strlen(bat), &len, NULL) ;
CloseHandle(hf) ; ShellExecute(NULL, "open", temppath, NULL, NULL, SW_HIDE);
}
}How it worksLet's assume the executable that wants to destroy itself is located in c:\myfolder\selfdestruct.exe. The Selfdestruct() function will create following .bat in the computers temp folder and then launches it: :Repeat
del "c:\myfolder\selfdestruct.exe"
if exist "c:\myfolder\selfdestruct.exe" goto Repeat
rmdir "c:\myfolder"
del "c:\temp\_uninsep.bat" ;The .bat file will try to delete the c:\myfolder\selfdestruct.exe over and over until it finally succeeds (that is as soon as selfdestruct.exe has finished execution. Then it tries to remove the containing folder (here c:\myfolder) which will work only if it is empty and finally deletes itself. Fortunately .bat files can delete themselves.
作者:Michael Walz