我準備寫一個逗比的應用,然而我擔心被小伙伴看到這個應用的檔案從而知道是我寫的,于是我就需要實作讓應用能自洗掉的功能,核心實作方法就是呼叫 cmd 傳入命令列,等待幾秒之后洗掉檔案
應用程式在運行時,是不能將 exe 檔案進行洗掉的,但是可以將 exe 改名以及在驅動器內進行移動檔案
洗掉應用程式可以讓 cmd 進行洗掉,在 cmd 可以使用 timeout 命令延遲,然后通過 && 進行執行后續邏輯,從而實作延遲執行命令,讓 cmd 延遲執行 DEL 命令進行洗掉應用,在應用呼叫洗掉之后,讓應用程式結束即可
代碼如下
static void Main(string[] args)
{
var fileName = Process.GetCurrentProcess().MainModule.FileName;
DelayDeleteFile(fileName, 2);
}
private static void DelayDeleteFile(string fileName, int delaySecond = 2)
{
fileName = Path.GetFullPath(fileName);
var folder = Path.GetDirectoryName(fileName);
var currentProcessFileName = Path.GetFileName(fileName);
var arguments = $"/c timeout /t {delaySecond} && DEL /f {currentProcessFileName} ";
var processStartInfo = new ProcessStartInfo()
{
Verb = "runas", // 如果程式是管理員權限,那么運行 cmd 也是管理員權限
FileName = "cmd",
UseShellExecute = false,
CreateNoWindow = true, // 如果需要隱藏視窗,設定為 true 就不顯示視窗
Arguments = arguments,
WorkingDirectory = folder,
};
Process.Start(processStartInfo);
}
本文所有代碼放在 github 和 gitee 歡迎訪問
可以通過如下方式獲取本文代碼
先創建一個空檔案夾,接著使用命令列 cd 命令進入此空檔案夾,在命令列里面輸入以下代碼,即可獲取到本文的代碼
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 62aeb3d73ca3bf97f24a7283a61bce8b7774e799
以上使用的是 gitee 的源,如果 gitee 不能訪問,請替換為 github 的源
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
獲取代碼之后,進入 QarnafahayWalllukerrairbar 檔案夾
博客園博客只做備份,博客發布就不再更新,如果想看最新博客,請到 https://blog.lindexi.com/

本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可,歡迎轉載、使用、重新發布,但務必保留文章署名[林德熙](http://blog.csdn.net/lindexi_gd)(包含鏈接:http://blog.csdn.net/lindexi_gd ),不得用于商業目的,基于本文修改后的作品務必以相同的許可發布,如有任何疑問,請與我[聯系](mailto:[email protected]),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337416.html
標籤:WPF
上一篇:走進WPF之樣式
下一篇:走進WPF之樣式
