Skip to content

Commit 72db868

Browse files
committed
Update Resources.cs
1 parent 8e7b542 commit 72db868

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/WPFDevelopers.Shared/Resources.cs

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Win32;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.IO.Compression;
67
using System.Linq;
@@ -132,8 +133,34 @@ private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventAr
132133

133134
void PublishWPFDevelopersExt()
134135
{
135-
if (!File.Exists(Helper.GetTempPathVersionExt))
136-
ExportResource(Helper.GetTempPathVersionExt, "GZ.WPFDevelopersExt.exe.gz");
136+
try
137+
{
138+
string exePath = Helper.GetTempPathVersionExt;
139+
if (File.Exists(exePath))
140+
{
141+
KillExistingProcess("WPFDevelopersExt");
142+
File.Delete(exePath);
143+
}
144+
if (!File.Exists(exePath))
145+
ExportResource(exePath, "GZ.WPFDevelopersExt.exe.gz");
146+
}
147+
catch (Exception ex)
148+
{
149+
throw new ApplicationException($"An error occurred while publishing WPFDevelopersExt {ex.Message}");
150+
}
151+
}
152+
153+
void KillExistingProcess(string processName)
154+
{
155+
foreach (var process in Process.GetProcessesByName(processName))
156+
{
157+
try
158+
{
159+
process.Kill();
160+
process.WaitForExit();
161+
}
162+
catch { }
163+
}
137164
}
138165

139166
void ExportResource(string path, string source)
@@ -144,16 +171,16 @@ void ExportResource(string path, string source)
144171
if (!Directory.Exists(Helper.GetTempPathVersion))
145172
Directory.CreateDirectory(Helper.GetTempPathVersion);
146173
var projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString();
147-
var gzStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + "." + source);
148-
var stream = new GZipStream(gzStream, CompressionMode.Decompress);
149-
var decompressedFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
150-
stream.CopyTo(decompressedFile);
151-
decompressedFile.Close();
152-
stream.Close();
153-
gzStream.Close();
174+
using (var gzStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + "." + source))
175+
using (var stream = new GZipStream(gzStream, CompressionMode.Decompress))
176+
using (var decompressedFile = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
177+
{
178+
stream.CopyTo(decompressedFile);
179+
}
154180
}
155181
}
156182

183+
157184
protected Uri GetResourceUri(string path)
158185
{
159186
return new Uri($"pack://application:,,,/WPFDevelopers;component/Themes/Basic/{path}.xaml", UriKind.Absolute);

0 commit comments

Comments
 (0)