欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中程序自刪除實現(xiàn)方法

 更新時間:2023年01月25日 14:54:16   作者:Danny_hi  
這篇文章主要介紹了C# 程序自刪除實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

C#程序自刪除

核心實現(xiàn)方法就是調(diào)用 cmd 傳入命令行,等待幾秒之后刪除文件;

應(yīng)用程序在運行時,是不能將 exe 文件進行刪除的。但是可以將 exe 改名以及在驅(qū)動器內(nèi)進行移動文件;

刪除應(yīng)用程序可以讓 cmd 進行刪除,在 cmd 可以使用 timeout 命令延遲,然后通過 && 進行執(zhí)行后續(xù)邏輯,從而實現(xiàn)延遲執(zhí)行命令。

讓 cmd 延遲執(zhí)行 DEL 命令進行刪除應(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)限,那么運行 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)限,那么運行 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)限,那么運行 cmd 也是管理員權(quán)限
? ? ? ? ? ? ? ? FileName = "cmd",
? ? ? ? ? ? ? ? UseShellExecute = false,
? ? ? ? ? ? ? ? CreateNoWindow = true, // 如果需要隱藏窗口,設(shè)置為 true 就不顯示窗口
? ? ? ? ? ? ? ? Arguments = arguments,
? ? ? ? ? ? ? ? WorkingDirectory = folder,
? ? ? ? ? ? };

? ? ? ? ? ? Process.Start(processStartInfo);
? ? ? ? }

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論