Программа которая обновляет сама себя

есть задача, написать программу на c#, исполняемый файл test.exe и есть новая версия программы которая лежит в папке new/test.exe вот написанный код: firstmethod запускается при открытии программы, secodmethd при нажатии на кнопку обновить

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace test
{
    class Class1
    {
        public Version version; //= typeof(Form1).Assembly.GetName().Version;
        public string s1 = "empty", s2 = "empty";
        public void FirstMethod()
        {
            if (File.Exists(@"..\test.exe")) { s1 = "yes"; }
            if (File.Exists(@"..\newtest.exe")) { s2 = "yes"; }

            //System.Threading.Thread.Sleep(4000);

            if (File.Exists(@"..\test.exe") && (File.Exists(@"..\newtest.exe")))
            {
                File.Delete(@"..\test.exe");
                File.Move(@"..\newtest.exe", @"..\test.exe");

                Process.Start(@"..\test.exe");
                System.Environment.Exit(0);
            }
        }

        public void SecondMethod()
        {
            if (File.Exists(@"new\test.exe"))
            {
                if (File.Exists(@"newtest.exe"))
                {
                    File.Delete(@"newtest.exe");
                }
                File.Copy(@"new\test.exe", "newtest.exe");
            }

            //System.Threading.Thread.Sleep(5000);

            //Process.Start(@"new\test.exe");

            Process cmd = new Process();//создаем новый объект класса
            cmd.StartInfo = new ProcessStartInfo(@"cmd.exe", @"C:\Users\UserPC\source\repos\test\test\bin\Debug\new\test.exe");
            cmd.Start();

            System.Environment.Exit(0);
        }
    }
}

я придумал при нажатии на кнопку обновить, создавать рядом с test.exe копию обновления под именем newtest.exe, затем открыть new/test.exe и из него удалить test.exe и переименовать newtest.exe, но файл new/test.exe не удаляет и не переименовывает файл. с чем может быть связано?


Ответы (0 шт):