C# 程序自删除方法

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);
        }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值