What's the best way for a .NET winforms application to update itself without using ClickOnce?

前端 未结 9 2375
孤独总比滥情好
孤独总比滥情好 2020-12-23 17:29

For technical reasons, I can\'t use ClickOnce to auto-update my .NET application and its assemblies. What is the best way to handle auto-updating in .NET?

相关标签:
9条回答
  • 2020-12-23 18:09

    On a project a long time ago, using .NET Compact Framework 1.0, I wrote an auto-updating application. We used SqlCE's CAB deployment feature to get the files onto the device (you would use Sync Framework now), and we had a separate exe that did the unpacking of the CAB, and updating the files.

    An update would go like this: the user would be prompted to update, click a button and drop out of the UI application. The updater exe would take over, get the cab file from the server, backup the current dlls and unpack the cab file with wceload. The UI would then be restarted, and if it failed, the update would be rolled back. This is still an interesting scenario on compact devices, but there are better tools now than just sqlce.

    I would certainly look at updater application block and sync framework to implement this if clickonce is not an option. But I'm guessing you'll still need a separate executable because the dlls you want to overwrite are probably file locked while in use by an exe, like one of the previous answers already said.

    0 讨论(0)
  • 2020-12-23 18:10

    Write your own.

    I have heard that they are somewhat difficult to write the first time, but after that it gets simple.

    Since I haven't written one yet (although its on my list), I can give you some of the things that I have thought of. Maintain accurate dll versions, as this is important for self updating. And make sure that the updater can update itself.

    0 讨论(0)
  • 2020-12-23 18:15

    In your Program.cs file do something like this:

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Update();
            Application.Run(new Form1());
        }
    
        private static void Update()
        {
            string mainFolder;
            string updateFolder;
            string backupFolder;
    
            foreach (string file in
                System.IO.Directory.GetFiles(updateFolder))
            {
                string newFile = file.Replace(
                    updateFolder, mainFolder);
    
                if (System.IO.File.Exists(newFile))
                {
                    System.IO.File.Replace(file, newFile, backupFolder);
                }
                else
                {
                    System.IO.File.Move(file, newFile);
                }
            }
        }
    

    Additionally, it can be made recursive to pick up directory structure if necessary. This will allow you to update any .dll in your project; everything, in fact, outside of the main .exe. Then somewhere else within your application you can deal with getting the files from your server (or wherever) that need to be updated, put then in the updateFolder and restart the application.

    0 讨论(0)
提交回复
热议问题