Skip to content

Commit b34f553

Browse files
committed
RestartExecutableName actually does what it should now
#187 -- more breaking changes in functionality, sorry, but things work as intended now!
1 parent 45af290 commit b34f553

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

src/NetSparkle.Samples.NetCore.WPF/MainWindow.xaml.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public MainWindow()
4040
_sparkle = new SparkleUpdater("https://netsparkleupdater.github.io/NetSparkle/files/sample-app/appcast.xml", new DSAChecker(Enums.SecurityMode.Strict))
4141
{
4242
UIFactory = new NetSparkleUpdater.UI.WPF.UIFactory(NetSparkleUpdater.UI.WPF.IconUtilities.ToImageSource(icon)),
43-
ShowsUIOnMainThread = false
43+
ShowsUIOnMainThread = false,
44+
//RelaunchAfterUpdate = true,
4445
//UseNotificationToast = true
4546
};
4647
// TLS 1.2 required by GitHub (https://developer.github.com/changes/2018-02-01-weak-crypto-removal-notice/)

src/NetSparkle.Samples.NetFramework.WPF/MainWindow.xaml.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public MainWindow()
2929
_sparkle = new SparkleUpdater("https://netsparkleupdater.github.io/NetSparkle/files/sample-app/appcast.xml", new DSAChecker(Enums.SecurityMode.Strict))
3030
{
3131
UIFactory = new NetSparkleUpdater.UI.WPF.UIFactory(NetSparkleUpdater.UI.WPF.IconUtilities.ToImageSource(icon)),
32-
ShowsUIOnMainThread = false
32+
ShowsUIOnMainThread = false,
33+
//RelaunchAfterUpdate = true,
3334
//UseNotificationToast = true
3435
};
3536
// TLS 1.2 required by GitHub (https://developer.github.com/changes/2018-02-01-weak-crypto-removal-notice/)

src/NetSparkle/SparkleUpdater.cs

+25-17
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ public string RestartExecutablePath
284284
/// This is the name that will be used/started when the update has been installed.
285285
/// This defaults to <see cref="Environment.CommandLine"/>.
286286
/// Used in conjunction with RestartExecutablePath to restart the application --
287-
/// {RestartExecutablePath}/{RestartExecutableName} is what is called to restart the
288-
/// app.
287+
/// cd "{RestartExecutablePath}"
288+
/// "{RestartExecutableName}" is what is called to restart the app.
289289
/// </summary>
290290
public string RestartExecutableName
291291
{
@@ -295,7 +295,20 @@ public string RestartExecutableName
295295
{
296296
return _restartExecutableName;
297297
}
298-
return Environment.CommandLine;
298+
#if NETCORE
299+
try
300+
{
301+
return Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
302+
}
303+
catch (Exception e)
304+
{
305+
LogWriter?.PrintMessage("Unable to get executable name: " + e.Message);
306+
}
307+
#endif
308+
// we cannot just use Path.GetFileName because on .NET Framework it can fail with
309+
// invalid chars in the path, so we do some crazy things to get the file name anotehr way
310+
var cmdLine = Environment.CommandLine.Trim().TrimStart('"').TrimEnd('"');
311+
return cmdLine.Substring(cmdLine.LastIndexOf(Path.DirectorySeparatorChar) + 1).Trim();
299312
}
300313
set
301314
{
@@ -415,7 +428,7 @@ public IAppCastHandler AppCastHandler
415428
set => _appCastHandler = value;
416429
}
417430

418-
#endregion
431+
#endregion
419432

420433
/// <summary>
421434
/// Starts a SparkleUpdater background loop to check for updates every 24 hours.
@@ -569,7 +582,7 @@ private void UnregisterEvents()
569582
}
570583
}
571584

572-
#endregion
585+
#endregion
573586

574587
/// <summary>
575588
/// This method checks if an update is required. During this process the appcast
@@ -1306,6 +1319,13 @@ protected virtual async Task RunDownloadedInstaller(string downloadFilePath)
13061319
LogWriter.PrintMessage("Generating batch in {0}", Path.GetFullPath(batchFilePath));
13071320

13081321
string processID = Process.GetCurrentProcess().Id.ToString();
1322+
string relaunchAfterUpdate = "";
1323+
if (RelaunchAfterUpdate)
1324+
{
1325+
relaunchAfterUpdate = $@"
1326+
cd ""{workingDir}""
1327+
""{executableName}""";
1328+
}
13091329

13101330
using (FileStream stream = new FileStream(batchFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true))
13111331
using (StreamWriter write = new StreamWriter(stream, new UTF8Encoding(false))/*new StreamWriter(batchFilePath, false, new UTF8Encoding(false))*/)
@@ -1315,13 +1335,6 @@ protected virtual async Task RunDownloadedInstaller(string downloadFilePath)
13151335
// We should wait until the host process has died before starting the installer.
13161336
// This way, any DLLs or other items can be replaced properly.
13171337
// Code from: http://stackoverflow.com/a/22559462/3938401
1318-
string relaunchAfterUpdate = "";
1319-
if (RelaunchAfterUpdate)
1320-
{
1321-
relaunchAfterUpdate = $@"
1322-
cd ""{workingDir}""
1323-
""{executableName}""";
1324-
}
13251338

13261339
string output = $@"
13271340
@echo off
@@ -1360,11 +1373,6 @@ setlocal ENABLEDELAYEDEXPANSION
13601373
fi;
13611374
done;
13621375
";
1363-
string relaunchAfterUpdate = "";
1364-
if (RelaunchAfterUpdate)
1365-
{
1366-
relaunchAfterUpdate = Path.Combine(workingDir, executableName);
1367-
}
13681376
if (IsZipDownload(downloadFilePath)) // .zip on macOS or .tar.gz on Linux
13691377
{
13701378
// waiting for finish based on http://blog.joncairns.com/2013/03/wait-for-a-unix-process-to-finish/

src/NetSparkle/Utilities.cs

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public static string GetFullBaseDirectory()
130130
return fullBaseDirectory;
131131
#else
132132
// https://stackoverflow.com/a/837501/3938401
133+
try
134+
{
135+
return Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
136+
}
137+
catch {}
133138
return System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
134139
#endif
135140
}

0 commit comments

Comments
 (0)