Skip to content

Commit

Permalink
Added ability set updated executable path from code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ravibpatel committed Apr 18, 2023
1 parent b847605 commit 5a9ad53
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 289 deletions.
4 changes: 2 additions & 2 deletions AutoUpdater.NET/AutoUpdater.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReleaseNotes>https://github.com/ravibpatel/AutoUpdater.NET/releases</PackageReleaseNotes>
<PackageOutputPath>build</PackageOutputPath>
<DocumentationFile>$(OutputPath)\$(Configuration)\AutoUpdater.NET.xml</DocumentationFile>
<LangVersion>default</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>build\lib</OutputPath>
Expand All @@ -51,6 +51,6 @@
<PackageReference Include="Resource.Embedder" Version="1.2.8" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1661.34" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1722.45" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions AutoUpdater.NET/AutoUpdater.NET.csproj.DotSettings

This file was deleted.

196 changes: 98 additions & 98 deletions AutoUpdater.NET/AutoUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public static class AutoUpdater
/// </summary>
public static string InstallationPath;

/// <summary>
/// If you are using a zip file as an update file, then you can set this value to a new executable path relative to the installation directory.
/// </summary>
public static string ExecutablePath;

/// <summary>
/// Set the Application Title shown in Update dialog. Although AutoUpdater.NET will get it automatically, you can set this property if you like to give custom Title.
/// </summary>
Expand Down Expand Up @@ -272,73 +277,71 @@ public static void Start(string appCast, Assembly myAssembly = null)
_remindLaterTimer = null;
}

if (!Running && _remindLaterTimer == null)
{
Running = true;
if (Running || _remindLaterTimer != null) return;

AppCastURL = appCast;
Running = true;

_isWinFormsApplication = Application.MessageLoop;
AppCastURL = appCast;

if (!_isWinFormsApplication)
{
Application.EnableVisualStyles();
}
_isWinFormsApplication = Application.MessageLoop;

Assembly assembly = myAssembly ?? Assembly.GetEntryAssembly();
if (!_isWinFormsApplication)
{
Application.EnableVisualStyles();
}

if (Synchronous)
{
try
{
var result = CheckUpdate(assembly);
Assembly assembly = myAssembly ?? Assembly.GetEntryAssembly();

if (StartUpdate(result))
{
return;
}
if (Synchronous)
{
try
{
var result = CheckUpdate(assembly);

Running = false;
}
catch (Exception exception)
if (StartUpdate(result))
{
ShowError(exception);
return;
}

Running = false;
}
else
catch (Exception exception)
{
using (var backgroundWorker = new BackgroundWorker())
{
backgroundWorker.DoWork += (_, args) =>
{
Assembly mainAssembly = args.Argument as Assembly;
ShowError(exception);
}
}
else
{
using var backgroundWorker = new BackgroundWorker();

backgroundWorker.DoWork += (_, args) =>
{
Assembly mainAssembly = args.Argument as Assembly;

args.Result = CheckUpdate(mainAssembly);
};
args.Result = CheckUpdate(mainAssembly);
};

backgroundWorker.RunWorkerCompleted += (_, args) =>
backgroundWorker.RunWorkerCompleted += (_, args) =>
{
if (args.Error != null)
{
ShowError(args.Error);
}
else
{
if (!args.Cancelled)
{
if (args.Error != null)
{
ShowError(args.Error);
}
else
if (StartUpdate(args.Result))
{
if (!args.Cancelled)
{
if (StartUpdate(args.Result))
{
return;
}
}

Running = false;
return;
}
};
}

backgroundWorker.RunWorkerAsync(assembly);
Running = false;
}
}
};

backgroundWorker.RunWorkerAsync(assembly);
}
}

Expand Down Expand Up @@ -446,47 +449,46 @@ private static bool StartUpdate(object result)
}
else
{
if (result is UpdateInfoEventArgs args)
if (result is not UpdateInfoEventArgs args) return false;

if (CheckForUpdateEvent != null)
{
if (CheckForUpdateEvent != null)
{
CheckForUpdateEvent(args);
}
else
CheckForUpdateEvent(args);
}
else
{
if (args.IsUpdateAvailable)
{
if (args.IsUpdateAvailable)
if (Mandatory && UpdateMode == Mode.ForcedDownload)
{
if (Mandatory && UpdateMode == Mode.ForcedDownload)
DownloadUpdate(args);
Exit();
}
else
{
if (Thread.CurrentThread.GetApartmentState().Equals(ApartmentState.STA))
{
DownloadUpdate(args);
Exit();
ShowUpdateForm(args);
}
else
{
if (Thread.CurrentThread.GetApartmentState().Equals(ApartmentState.STA))
{
ShowUpdateForm(args);
}
else
{
Thread thread = new Thread(new ThreadStart(delegate { ShowUpdateForm(args); }));
thread.CurrentCulture =
thread.CurrentUICulture = CultureInfo.CurrentCulture;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
Thread thread = new Thread(new ThreadStart(delegate { ShowUpdateForm(args); }));
thread.CurrentCulture =
thread.CurrentUICulture = CultureInfo.CurrentCulture;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}

return true;
}

if (ReportErrors)
{
MessageBox.Show(Resources.UpdateUnavailableMessage,
Resources.UpdateUnavailableCaption,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return true;
}

if (ReportErrors)
{
MessageBox.Show(Resources.UpdateUnavailableMessage,
Resources.UpdateUnavailableCaption,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Expand Down Expand Up @@ -553,7 +555,7 @@ internal static void Exit()

if (!process.HasExited)
{
process.Kill(); //TODO show UI message asking user to close program himself instead of silently killing it
process.Kill(); //TODO: Show UI message asking user to close program himself instead of silently killing it
}
}
}
Expand Down Expand Up @@ -637,15 +639,14 @@ internal static void SetTimer(DateTime remindLater)
/// </summary>
public static bool DownloadUpdate(UpdateInfoEventArgs args)
{
using (var downloadDialog = new DownloadUpdateDialog(args))
using var downloadDialog = new DownloadUpdateDialog(args);

try
{
return downloadDialog.ShowDialog().Equals(DialogResult.OK);
}
catch (TargetInvocationException)
{
try
{
return downloadDialog.ShowDialog().Equals(DialogResult.OK);
}
catch (TargetInvocationException)
{
}
}

return false;
Expand All @@ -656,17 +657,16 @@ public static bool DownloadUpdate(UpdateInfoEventArgs args)
/// </summary>
public static void ShowUpdateForm(UpdateInfoEventArgs args)
{
using (var updateForm = new UpdateForm(args))
using var updateForm = new UpdateForm(args);

if (UpdateFormSize.HasValue)
{
if (UpdateFormSize.HasValue)
{
updateForm.Size = UpdateFormSize.Value;
}
updateForm.Size = UpdateFormSize.Value;
}

if (updateForm.ShowDialog().Equals(DialogResult.OK))
{
Exit();
}
if (updateForm.ShowDialog().Equals(DialogResult.OK))
{
Exit();
}
}

Expand Down
Loading

0 comments on commit 5a9ad53

Please sign in to comment.