Skip to content

Commit 138885c

Browse files
committed
Cleanup old config when a new version runs the first time. Also strong sign the assembly so the config path is consistent.
1 parent 075bd01 commit 138885c

6 files changed

Lines changed: 76 additions & 7 deletions

File tree

Rapr/Program.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Configuration;
23
using System.IO;
34
using System.Reflection;
5+
using System.Runtime.CompilerServices;
6+
using System.Security;
47
using System.Windows.Forms;
58

69
namespace Rapr
@@ -32,6 +35,29 @@ private static void AddEnvironmentPaths(string path)
3235
Environment.SetEnvironmentVariable("PATH", newPath);
3336
}
3437

38+
private static void CleanUpOldConfig()
39+
{
40+
FileInfo fileInfo = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);
41+
DirectoryInfo strongAssemblyConfigDirectory = fileInfo.Directory.Parent;
42+
43+
// Remove old version of config.
44+
foreach (var dir in strongAssemblyConfigDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
45+
{
46+
if (Version.TryParse(dir.Name, out Version version) && version < Assembly.GetExecutingAssembly().GetName().Version)
47+
{
48+
dir.Delete(recursive: true);
49+
}
50+
}
51+
52+
DirectoryInfo configDirectory = strongAssemblyConfigDirectory.Parent;
53+
54+
// Remove configurations created by not strong signed app.
55+
foreach (var dir in configDirectory.EnumerateDirectories(Assembly.GetEntryAssembly().ManifestModule.Name + "_Url_*", SearchOption.TopDirectoryOnly))
56+
{
57+
dir.Delete(recursive: true);
58+
}
59+
}
60+
3561
/// <summary>
3662
/// The main entry point for the application.
3763
/// </summary>
@@ -42,6 +68,24 @@ public static void Main()
4268
AppDomain.CurrentDomain.AssemblyResolve += ResolveEventHandler;
4369
Application.EnableVisualStyles();
4470
Application.SetCompatibleTextRenderingDefault(false);
71+
72+
if (Properties.Settings.Default.UpgradeRequired)
73+
{
74+
Properties.Settings.Default.Upgrade();
75+
Properties.Settings.Default.UpgradeRequired = false;
76+
Properties.Settings.Default.Save();
77+
78+
try
79+
{
80+
CleanUpOldConfig();
81+
}
82+
catch (Exception e) when (
83+
e is SecurityException
84+
|| e is IOException)
85+
{
86+
}
87+
}
88+
4589
using (DSEForm mainForm = new DSEForm())
4690
{
4791
Application.Run(mainForm);

Rapr/Properties/Settings.Designer.cs

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rapr/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
<Setting Name="DriverStoreViewState" Type="System.String" Scope="User">
1818
<Value Profile="(Default)" />
1919
</Setting>
20+
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
21+
<Value Profile="(Default)">True</Value>
22+
</Setting>
2023
</Settings>
2124
</SettingsFile>

Rapr/Rapr.csproj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,23 @@
3838
<ApplicationManifest>app.manifest</ApplicationManifest>
3939
<CopyNuGetImplementations>false</CopyNuGetImplementations>
4040
</PropertyGroup>
41+
<PropertyGroup>
42+
<SignAssembly>true</SignAssembly>
43+
</PropertyGroup>
44+
<PropertyGroup>
45+
<AssemblyOriginatorKeyFile>Rapr.snk</AssemblyOriginatorKeyFile>
46+
</PropertyGroup>
4147
<ItemGroup>
42-
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4">
48+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.7">
4349
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4450
<PrivateAssets>all</PrivateAssets>
4551
</PackageReference>
4652
<PackageReference Include="Microsoft.Dism" Version="2.0.20" GeneratePathProperty="true" />
53+
<PackageReference Include="Microsoft.WindowsAPICodePack.Core" Version="1.1.0" GeneratePathProperty="true" />
54+
<PackageReference Include="Microsoft.WindowsAPICodePack.Shell" Version="1.1.0" GeneratePathProperty="true" />
4755
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" GeneratePathProperty="true" />
4856
<PackageReference Include="ObjectListView.Official" Version="2.9.1" GeneratePathProperty="true" />
4957
<PackageReference Include="System.ValueTuple" Version="4.5.0" GeneratePathProperty="true" />
50-
<PackageReference Include="WindowsAPICodePack-Core" Version="1.1.1" GeneratePathProperty="true" />
51-
<PackageReference Include="WindowsAPICodePack-Shell" Version="1.1.1" GeneratePathProperty="true" />
5258
</ItemGroup>
5359
<ItemGroup>
5460
<Reference Include="System" />
@@ -164,14 +170,15 @@
164170
<DependentUpon>Settings.settings</DependentUpon>
165171
<DesignTimeSharedInput>True</DesignTimeSharedInput>
166172
</Compile>
173+
<None Include="Rapr.snk" />
167174
</ItemGroup>
168175
<ItemGroup>
169176
<EmbeddedResource Include="$(PkgMicrosoft_Dism)\lib\net40\Microsoft.Dism.dll" />
170177
<EmbeddedResource Include="$(PkgNewtonsoft_Json)\lib\net45\Newtonsoft.Json.dll" />
171178
<EmbeddedResource Include="$(PkgObjectListView_Official)\lib\net20\ObjectListView.dll" />
172179
<EmbeddedResource Include="$(PkgSystem_ValueTuple)\lib\netstandard1.0\System.ValueTuple.dll" />
173-
<EmbeddedResource Include="$(PkgWindowsAPICodePack-Core)\lib\Microsoft.WindowsAPICodePack.dll" />
174-
<EmbeddedResource Include="$(PkgWindowsAPICodePack-Shell)\lib\Microsoft.WindowsAPICodePack.Shell.dll" />
180+
<EmbeddedResource Include="$(PkgMicrosoft_WindowsAPICodePack_Core)\lib\Microsoft.WindowsAPICodePack.dll" />
181+
<EmbeddedResource Include="$(PkgMicrosoft_WindowsAPICodePack_Shell)\lib\Microsoft.WindowsAPICodePack.Shell.dll" />
175182
</ItemGroup>
176183
<ItemGroup>
177184
<Content Include="icon.ico" />

Rapr/Rapr.snk

596 Bytes
Binary file not shown.

Rapr/app.config

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
<value>Normal</value>
3131
</setting>
3232
<setting name="DriverStoreViewState" serializeAs="String">
33-
<value/>
33+
<value />
34+
</setting>
35+
<setting name="UpgradeRequired" serializeAs="String">
36+
<value>True</value>
3437
</setting>
3538
</Rapr.Properties.Settings>
3639
</userSettings>

0 commit comments

Comments
 (0)