使用将调用Windows服务的控制台应用程序更新WPF应用程序

时间:2018-10-09 03:43:25

标签: c# wpf windows-services console-application

您好stackoverflow大师。我想尝试解决的是更新我的WPF应用程序,我有3个程序WPF应用程序,控制台应用程序和Windows服务。它所做的是我的WPF应用程序调用了我的控制台应用程序,而我的控制台应用程序向Windows服务发送了一个命令,我的Windows服务将终止WPF应用程序的进程并进行更新,并且我的控制台应用程序将再次运行WPF应用程序。现在,Windows服务中的更新部分正在运行,但是我的控制台应用程序存在问题,因为WPF应用程序每次用户打开它都会调用我的控制台应用程序,并且它永远循环。我知道控制台应用程序存在问题。

预先感谢大师。 :)

这是我的代码。

try
{
_log.Info("Checking for new update.");
int wpfAppExecuteCommand;
string defaultpath = @"C:\Program Files (x86)\WPF Application";
string wpfAppInfoPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WPF\\WPFAppInfo.txt");
string[] txtFile = File.ReadAllLines(wpfAppInfoPath);

if (txtFile.Count() > 0)
{
    _wpfAppInfo = txtFile[0];

    switch (_wpfAppInfo)
    {
        case "WPF Application":
            wpfAppExecuteCommand = 150;
            _wpfSoftwareLocation = Path.Combine(defaultpath, "WPF Application.exe");
            break;
        default:
            wpfAppExecuteCommand = 0;
            break;
    }

    using (var service = new ServiceController("Update Checker API Service"))
    {
        if (service.Status == ServiceControllerStatus.Running)
        {
            _log.Debug("Service is running.");
            service.ExecuteCommand(wpfAppExecuteCommand);
        }
        else
        {
            _log.Error("Service is not running.");
        }
    }
}

_log.Debug("Please wait..");
_stopwatch = new Stopwatch();
_stopwatch.Start();

bool checking = true;

while (checking)
{
    if (_stopwatch.Elapsed > TimeSpan.FromMinutes(1))
    {
        if (!_oneMin)
        {
            _oneMin = true;
            _log.Warn("Installing udpate is taking too long, please wait..");
        }
    }
    else if (_stopwatch.Elapsed > TimeSpan.FromMinutes(5))
    {
        if (!_fiveMin)
        {
            _fiveMin = true;
            _log.Warn("Installing udpate is taking too long, please wait..");
        }
    }
    else if (_stopwatch.Elapsed > TimeSpan.FromMinutes(10))
    {
        if (!_tenMin)
        {
            _tenMin = true;
            _isTimerOn = true;
            _log.Warn(".");
        }
    }

    if (File.Exists(_resultTxt))
    {
        _log.Trace("File exist. Read if is Success");
        string[] lines = File.ReadAllLines(_resultTxt);

        if (lines[0].Equals("Success"))
        {
            _log.Trace("Checks if the Software.exe exist");
            if (File.Exists(_wpfSoftwareLocation))
            {
                string runMsiExecBatchFile = Path.Combine(_batchFileDefaultFolder, "RunMsiExec.bat");

                //Deleting the batch file used by windows service.
                if (File.Exists(runMsiExecBatchFile))
                {
                    File.Delete(Path.Combine(_batchFileDefaultFolder, "RunMsiExec.bat"));
                    _log.Info("RunMsiExec.bat file deleted.");
                }
                else
                {
                    _log.Warn("RunMsiExec.bat not found");
                }

                _openWpfApp = true;
                checking = false;
                _stopwatch.Stop();
            }
            else
            {
                _log.Error("Software.exe not exist");
                _stopwatch.Stop();
                checking = false;
            }

        }
        else if (lines[0].Equals("The current version is up to date"))
        {
            _log.Info("The current version is up to date");
            _openWpfApp = true;
            checking = false;
            _stopwatch.Stop();
        }
        else
        {
            _log.Error(lines[0]);
            checking = false;
            _stopwatch.Stop();
        }
    }
    else
    {
        _log.Debug("Result text not found.");
    }
}

if (_openWpfApp)
{
    _log.Info("Open WPF application software.");
    Process.Start(_wpfSoftwareLocation);
}
else
{
    _log.Info("Unable to open the Wpf application software.");
} 
catch (Exception ex)
{
_log.Error(ex);
}

0 个答案:

没有答案