C#built .exe是否可以自行删除?

时间:2013-10-30 16:52:38

标签: c#

我构建了一个基本的Windows窗体应用程序。我想这样做,以便我的程序在我选择的日期之后删除。

具体来说,当有人点击.exe来运行它时,如果是在特定日期之后,则会删除.exe。这可能吗?如果是,我该怎么做?

我认为我的代码看起来像这样:

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

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

7 个答案:

答案 0 :(得分:11)

这样可行,运行命令行操作来删除自己。

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"
});

答案 1 :(得分:7)

您必须确保在要删除文件时已关闭应用程序。我会建议类似下面的内容 - 当然你需要做一些修改。

以下示例适用于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");
}

答案 2 :(得分:3)

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

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

答案 3 :(得分:2)

程序一般不能删除本身。从taskmgr开始考虑它。

您看到myprogram.exe正在投放,myprogram.exe尝试删除myprogram.exe您能想到问题吗?我可以......它已经在使用中了。

这就是为什么我们看到卸载程序的卸载程序。

答案 4 :(得分:1)

是。例如,启动定时批处理作业以在终止后删除exe。但是,您无法确保您的exe确实被删除。

您的exe在运行时无法删除。

这不起作用:

System.IO.File.Delete(System.AppDomain.CurrentDomain.FriendlyName);

答案 5 :(得分:0)

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();
            }
}

答案 6 :(得分:-1)

尝试这样的事情:

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" });
  }
}