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

前端之家收集整理的这篇文章主要介绍了C#built .exe是否可以自行删除?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我构建了一个基本的 Windows窗体应用程序我想这样做,以便我的程序在我选择的日期之后删除.

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

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

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

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

解决方法

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

以下示例适用于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");
}
原文链接:https://www.f2er.com/c/117658.html

猜你在找的C&C++相关文章