網(wǎng)站首頁 編程語言 正文
C#程序自刪除
核心實(shí)現(xiàn)方法就是調(diào)用 cmd 傳入命令行,等待幾秒之后刪除文件;
應(yīng)用程序在運(yùn)行時(shí),是不能將 exe 文件進(jìn)行刪除的。但是可以將 exe 改名以及在驅(qū)動(dòng)器內(nèi)進(jìn)行移動(dòng)文件;
刪除應(yīng)用程序可以讓 cmd 進(jìn)行刪除,在 cmd 可以使用 timeout 命令延遲,然后通過 && 進(jìn)行執(zhí)行后續(xù)邏輯,從而實(shí)現(xiàn)延遲執(zhí)行命令。
讓 cmd 延遲執(zhí)行 DEL 命令進(jìn)行刪除應(yīng)用,在應(yīng)用調(diào)用刪除之后,讓應(yīng)用程序結(jié)束即可
代碼如下
static void Main(string[] args)
{
? ? ?var fileName = Process.GetCurrentProcess().MainModule.FileName;
? ? ?DelayDeleteFile(fileName, 2);?? ?//這里是關(guān)閉程序后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", // 如果程序是管理員權(quán)限,那么運(yùn)行 cmd 也是管理員權(quán)限
? ? ? ? ? FileName = "cmd",
? ? ? ? ? UseShellExecute = false,
? ? ? ? ? CreateNoWindow = true, // 如果需要隱藏窗口,設(shè)置為 true 就不顯示窗口
? ? ? ? ? Arguments = arguments,
? ? ? ? ? WorkingDirectory = folder,
? ? ?};
? ? ?Process.Start(processStartInfo);
}
Winform使用示例
static void Main()
? ? ? ? {
? ? ? ? ? ? Application.EnableVisualStyles();
? ? ? ? ? ? Application.SetCompatibleTextRenderingDefault(false);
? ? ? ? ? ? Application.Run(new Form1());
? ? ? ? ? ? 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", // 如果程序是管理員權(quán)限,那么運(yùn)行 cmd 也是管理員權(quán)限
? ? ? ? ? ? ? ? FileName = "cmd",
? ? ? ? ? ? ? ? UseShellExecute = false,
? ? ? ? ? ? ? ? CreateNoWindow = true, // 如果需要隱藏窗口,設(shè)置為 true 就不顯示窗口
? ? ? ? ? ? ? ? Arguments = arguments,
? ? ? ? ? ? ? ? WorkingDirectory = folder,
? ? ? ? ? ? };
? ? ? ? ? ? Process.Start(processStartInfo);
? ? ? ? }
WPF使用示例
首先在app.xaml中添加ShutdownMode=“OnExplicitShutdown”,刪除StartupUri=“MainWindow.xaml”
然后在app.xaml.cs中添加如下代碼:
protected override void OnStartup(StartupEventArgs e)
? ? ? ? {
? ? ? ? ? ? base.OnStartup(e);
? ? ? ? ? ? new MainWindow().ShowDialog();
? ? ? ? ? ? var fileName = Process.GetCurrentProcess().MainModule.FileName;
? ? ? ? ? ? DelayDeleteFile(fileName, 2);
? ? ? ? ? ? Application.Current.Shutdown();
? ? ? ? }
? ? ? ? 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", // 如果程序是管理員權(quán)限,那么運(yùn)行 cmd 也是管理員權(quán)限
? ? ? ? ? ? ? ? FileName = "cmd",
? ? ? ? ? ? ? ? UseShellExecute = false,
? ? ? ? ? ? ? ? CreateNoWindow = true, // 如果需要隱藏窗口,設(shè)置為 true 就不顯示窗口
? ? ? ? ? ? ? ? Arguments = arguments,
? ? ? ? ? ? ? ? WorkingDirectory = folder,
? ? ? ? ? ? };
? ? ? ? ? ? Process.Start(processStartInfo);
? ? ? ? }
總結(jié)
原文鏈接:https://blog.csdn.net/qq_43024228/article/details/122404162
相關(guān)推薦
- 2022-07-06 c#?模擬串口通信?SerialPort的實(shí)現(xiàn)示例_C#教程
- 2023-01-23 Python操作MongoDB增刪改查代碼示例_python
- 2023-06-16 GO的鎖和原子操作的示例詳解_Golang
- 2022-12-05 Django中QuerySet查詢優(yōu)化之prefetch_related詳解_python
- 2022-06-12 python?包之?re?正則匹配教程分享_python
- 2021-12-15 Android?studio導(dǎo)出APP測(cè)試包和構(gòu)建正式簽名包_Android
- 2022-06-01 Python+Opencv實(shí)現(xiàn)計(jì)算閉合區(qū)域面積_python
- 2022-09-08 Python中LSTM回歸神經(jīng)網(wǎng)絡(luò)時(shí)間序列預(yù)測(cè)詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支