10

I built a basic Windows Form app. I want to make it so that my program deletes itself after a date of my choosing.

Specifically, when someone clicks on the .exe to run it, and if it's after a certain date, the .exe is deleted. Is this possible? If yes, how do I do this?

I think my code would look something like this:

DateTime expiration = new DateTime(2013, 10, 31) //Oct. 31, 2013

If (DateTime.Today > expiration) 
{
    //command to self-delete
}
else  
{
    //proceed normally
}
4

7 回答 7

20

这有效,运行命令行操作来删除自己。

Process.Start( new ProcessStartInfo()
{
    Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + Application.ExecutablePath +"\"",
    WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, FileName = "cmd.exe"
});
于 2014-06-03T20:05:54.060 回答
9

当您要删除文件时,您必须确保您的应用程序已经关闭。我会建议类似于以下内容的内容-您当然需要进行一些修改。

以下示例适用于 windows,需要针对其他操作系统进行修改。

/// <summary>
/// Represents the entry point of our application.
/// </summary>
/// <param name="args">Possibly spcified command line arguments.</param>
public static void Main(string[] args)
{
    string batchCommands = string.Empty;
    string exeFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///",string.Empty).Replace("/","\\");

    batchCommands += "@ECHO OFF\n";                         // Do not show any output
    batchCommands += "ping 127.0.0.1 > nul\n";              // Wait approximately 4 seconds (so that the process is already terminated)
    batchCommands += "echo j | del /F ";                    // Delete the executeable
    batchCommands += exeFileName + "\n";
    batchCommands += "echo j | del deleteMyProgram.bat";    // Delete this bat file

    File.WriteAllText("deleteMyProgram.bat", batchCommands);

    Process.Start("deleteMyProgram.bat");
}
于 2013-10-30T17:09:02.987 回答
4

它在运行时无法自行删除,因为它的可执行文件和库文件将被锁定。您可以编写第二个程序,将进程 ID 作为参数,等待该进程终止,然后删除第一个程序。将其作为资源嵌入到您的主应用程序中,然后将其提取到%TEMP%、运行并退出。

这种技术通常与自动更新程序结合使用,而且绝对不是万无一失的。

于 2013-10-30T16:57:13.413 回答
3

程序一般不可能自行删除。从taskmgr的角度考虑一下。

你看到myprogram.exe正在运行,并myprogram.exe试图删除myprogram.exe 我能想到的一个问题myprogram.exe是已经在运行,导致访问问题。

这就是为什么我们看到卸载程序会删除所有内容。它作为一个单独的可执行文件运行。

于 2013-10-30T16:55:12.680 回答
1

是的。例如,启动定时批处理作业以在 exe 终止后删除它。但是你不能确保你的 exe 真的被删除了。

您不能在运行时删除您的 exe。

这不起作用:

System.IO.File.Delete(System.AppDomain.CurrentDomain.FriendlyName);
于 2013-10-30T16:57:34.237 回答
0

尝试这样的事情:

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
//Include a reference to system

class MyClass {
  static void Main() {
    InitiateSelfDestructSequence();
    Thread.Sleep(10000);
  }
  static void InitiateSelfDestructSequence() {
    string batchScriptName = "BatchScript.bat";
    using (StreamWriter writer = File.AppendText(batchScriptName)) {
      writer.WriteLine(":Loop");
      writer.WriteLine("Tasklist /fi \"PID eq " + Process.GetCurrentProcess().Id.ToString() + "\" | find \":\"");
      writer.WriteLine("if Errorlevel 1 (");
      writer.WriteLine("  Timeout /T 1 /Nobreak");
      writer.WriteLine("  Goto Loop");
      writer.WriteLine(")");
      writer.WriteLine("Del \"" + (new FileInfo((new Uri(Assembly.GetExecutingAssembly().CodeBase)).LocalPath)).Name + "\"");
    }
    Process.Start(new ProcessStartInfo() { Arguments = "/C " + batchScriptName + " & Del " + batchScriptName, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, FileName = "cmd.exe" });
  }
}
于 2016-05-16T23:55:57.193 回答
-1
void Form1Load(object sender, System.EventArgs e)
{
      if (System.IO.File.Exists("C:/myfiles.exe"))
            {
                    if (System.IO.File.Exists("C:/text.txt"))
                    {
                    read    c:/text.txt     
                    and System.IO.File.Delete("   read data   ")
                    and System.IO.File.Delete("c:/text.txt")
                    }               
            }
            else
            {
            string exePath = Application.ExecutablePath;
            System.IO.File.Copy(exePath, @"C:/myfiles.exe");
                   and create c:/text.txt 
                     in write....  " exepath "
                   and c:/myfiles.exe run code
            Application.Exit();
            }
}
于 2014-05-20T02:20:40.477 回答