日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#中程序自刪除實現方法_C#教程

作者:Danny_hi ? 更新時間: 2023-03-20 編程語言

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

欄目分類
最近更新