【问题标题】:Is it possible for a C# built .exe to self-delete?C# 构建的 .exe 是否可以自删除?
【发布时间】:2013-10-30 16:52:38
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 不应该由用户决定何时删除或卸载某些东西吗?
  • 只需触发另一个进程将其删除 - 在您的主程序退出后运行
  • @Amy 不,我希望我的小程序能够像电影中一样自毁!或者,至少,扔进回收站。
  • 启动另一个进程将其删除。用户打开你的应用程序,它看到它已经过了某个日期,它启动了其他东西,退出,“其他东西”删除了应用程序。它可以像批处理文件一样简单!

标签: c#


【解决方案1】:

这行得通,运行命令行操作来删除自己。

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

【讨论】:

  • 你能详细说明这些论点吗?
  • @Chad choice /C Y /N /D Y /T 3 等待 3 秒然后继续删除应用程序
  • 在某些情况下,Assembly.GetExecutingAssembly().CodeBaseApplication.ExecutablePath 更好(查看这里的讨论到这个答案:stackoverflow.com/a/3991953/1574221
  • 我个人更喜欢Process.GetCurrentProcess().MainModule.FileName 另外请注意,此代码后面应该是Application.Exit() 或者放在结束​​事件中
【解决方案2】:

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

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

【讨论】:

  • echo j 是干什么用的?
  • 其实你也可以尝试使用del /F /Q'. echo j`作为输入传递给del /F,在德语中代表Ja。如果您的操作系统是英文操作系统,您可能需要使用echo y。整个事情都是一种解决方法,以防del /Q 无法正确完成。
【解决方案3】:

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

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

【讨论】:

  • 是的,这是唯一的方法。我的答案或多或少完全相同,但就像你第一次一样,我会删除我的。
【解决方案4】:

程序一般不可能自行删除。站在taskmgr的角度思考一下。

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

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

【讨论】:

    【解决方案5】:

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

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

    这行不通:

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

    【讨论】:

      【解决方案6】:

      试试这样的:

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

      【讨论】:

        【解决方案7】:
        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();
                    }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-05-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-04
          • 1970-01-01
          • 2012-06-24
          • 1970-01-01
          • 2011-04-16
          • 1970-01-01
          相关资源
          最近更新 更多