網站首頁 編程語言 正文
C#程序自刪除
核心實現方法就是調用 cmd 傳入命令行,等待幾秒之后刪除文件;
應用程序在運行時,是不能將 exe 文件進行刪除的。但是可以將 exe 改名以及在驅動器內進行移動文件;
刪除應用程序可以讓 cmd 進行刪除,在 cmd 可以使用 timeout 命令延遲,然后通過 && 進行執行后續邏輯,從而實現延遲執行命令。
讓 cmd 延遲執行 DEL 命令進行刪除應用,在應用調用刪除之后,讓應用程序結束即可
代碼如下
static void Main(string[] args)
{
? ? ?var fileName = Process.GetCurrentProcess().MainModule.FileName;
? ? ?DelayDeleteFile(fileName, 2);?? ?//這里是關閉程序后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);
}
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", // 如果程序是管理員權限,那么運行 cmd 也是管理員權限
? ? ? ? ? ? ? ? FileName = "cmd",
? ? ? ? ? ? ? ? UseShellExecute = false,
? ? ? ? ? ? ? ? CreateNoWindow = true, // 如果需要隱藏窗口,設置為 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", // 如果程序是管理員權限,那么運行 cmd 也是管理員權限
? ? ? ? ? ? ? ? FileName = "cmd",
? ? ? ? ? ? ? ? UseShellExecute = false,
? ? ? ? ? ? ? ? CreateNoWindow = true, // 如果需要隱藏窗口,設置為 true 就不顯示窗口
? ? ? ? ? ? ? ? Arguments = arguments,
? ? ? ? ? ? ? ? WorkingDirectory = folder,
? ? ? ? ? ? };
? ? ? ? ? ? Process.Start(processStartInfo);
? ? ? ? }
總結
原文鏈接:https://blog.csdn.net/qq_43024228/article/details/122404162
- 上一篇:C#實現簡單的文件加密與解密方式_C#教程
- 下一篇:C#中如何生成安裝包_C#教程
相關推薦
- 2022-06-22 Android使用http實現注冊登錄功能_Android
- 2022-09-25 Shiro和SpringSecurity
- 2022-06-06 flutter 布局管理詳解
- 2022-12-12 flutter?InheritedWidget使用方法總結_Android
- 2022-05-03 Shell內置命令之exit的語法與實例_linux shell
- 2022-06-22 Git?Bash終端默認路徑的設置查看修改及拓展圖文詳解_其它綜合
- 2023-03-16 Android藍牙服務查找附近設備分析探索_Android
- 2022-06-06 在使用element-ui中的el-table組件時,怎么自定義合并表頭和合并行
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支