1
1
using Microsoft . Win32 ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Diagnostics ;
4
5
using System . IO ;
5
6
using System . IO . Compression ;
6
7
using System . Linq ;
@@ -132,8 +133,34 @@ private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventAr
132
133
133
134
void PublishWPFDevelopersExt ( )
134
135
{
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
+ }
137
164
}
138
165
139
166
void ExportResource ( string path , string source )
@@ -144,16 +171,16 @@ void ExportResource(string path, string source)
144
171
if ( ! Directory . Exists ( Helper . GetTempPathVersion ) )
145
172
Directory . CreateDirectory ( Helper . GetTempPathVersion ) ;
146
173
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
+ }
154
180
}
155
181
}
156
182
183
+
157
184
protected Uri GetResourceUri ( string path )
158
185
{
159
186
return new Uri ( $ "pack://application:,,,/WPFDevelopers;component/Themes/Basic/{ path } .xaml", UriKind . Absolute ) ;
0 commit comments