Self deletable application in C# in one executable

前端 未结 7 1825
北荒
北荒 2020-11-30 23:26

Is it possible to make an application in C# that will be able to delete itself in some condition.

I need to write an updater for my application but I don\'t want the

7条回答
  •  长情又很酷
    2020-11-30 23:33

    I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards

    public static class Updater
    {
        public static void Main() 
        {   
            string path = @"updater.bat";
    
            if (!File.Exists(path)) 
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path)) 
                {
                    sw.WriteLine("updater.exe");
                    sw.WriteLine("delete updater.exe /y");
                    sw.WriteLine("delete updater.bat /y");
                } 
    
                System.Process.Start(path);   
            }
            else
            {
                RunUpdateProcess();
            }
        }
    
        private void RunUpdateProcess()
        {
            .....
        }
    }
    

提交回复
热议问题