Skip to content

Commit

Permalink
Cleanup old config when a new version runs the first time. Also stron…
Browse files Browse the repository at this point in the history
…g sign the assembly so the config path is consistent.
  • Loading branch information
Teddy Zhang committed Nov 7, 2019
1 parent 075bd01 commit 138885c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
44 changes: 44 additions & 0 deletions Rapr/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Windows.Forms;

namespace Rapr
Expand Down Expand Up @@ -32,6 +35,29 @@ private static void AddEnvironmentPaths(string path)
Environment.SetEnvironmentVariable("PATH", newPath);
}

private static void CleanUpOldConfig()
{
FileInfo fileInfo = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);
DirectoryInfo strongAssemblyConfigDirectory = fileInfo.Directory.Parent;

// Remove old version of config.
foreach (var dir in strongAssemblyConfigDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
{
if (Version.TryParse(dir.Name, out Version version) && version < Assembly.GetExecutingAssembly().GetName().Version)
{
dir.Delete(recursive: true);
}
}

DirectoryInfo configDirectory = strongAssemblyConfigDirectory.Parent;

// Remove configurations created by not strong signed app.
foreach (var dir in configDirectory.EnumerateDirectories(Assembly.GetEntryAssembly().ManifestModule.Name + "_Url_*", SearchOption.TopDirectoryOnly))
{
dir.Delete(recursive: true);
}
}

/// <summary>
/// The main entry point for the application.
/// </summary>
Expand All @@ -42,6 +68,24 @@ public static void Main()
AppDomain.CurrentDomain.AssemblyResolve += ResolveEventHandler;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

if (Properties.Settings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();

try
{
CleanUpOldConfig();
}
catch (Exception e) when (
e is SecurityException
|| e is IOException)
{
}
}

using (DSEForm mainForm = new DSEForm())
{
Application.Run(mainForm);
Expand Down
14 changes: 13 additions & 1 deletion Rapr/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Rapr/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
<Setting Name="DriverStoreViewState" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
17 changes: 12 additions & 5 deletions Rapr/Rapr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<CopyNuGetImplementations>false</CopyNuGetImplementations>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>Rapr.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Dism" Version="2.0.20" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.WindowsAPICodePack.Core" Version="1.1.0" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.WindowsAPICodePack.Shell" Version="1.1.0" GeneratePathProperty="true" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" GeneratePathProperty="true" />
<PackageReference Include="ObjectListView.Official" Version="2.9.1" GeneratePathProperty="true" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" GeneratePathProperty="true" />
<PackageReference Include="WindowsAPICodePack-Core" Version="1.1.1" GeneratePathProperty="true" />
<PackageReference Include="WindowsAPICodePack-Shell" Version="1.1.1" GeneratePathProperty="true" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -164,14 +170,15 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<None Include="Rapr.snk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(PkgMicrosoft_Dism)\lib\net40\Microsoft.Dism.dll" />
<EmbeddedResource Include="$(PkgNewtonsoft_Json)\lib\net45\Newtonsoft.Json.dll" />
<EmbeddedResource Include="$(PkgObjectListView_Official)\lib\net20\ObjectListView.dll" />
<EmbeddedResource Include="$(PkgSystem_ValueTuple)\lib\netstandard1.0\System.ValueTuple.dll" />
<EmbeddedResource Include="$(PkgWindowsAPICodePack-Core)\lib\Microsoft.WindowsAPICodePack.dll" />
<EmbeddedResource Include="$(PkgWindowsAPICodePack-Shell)\lib\Microsoft.WindowsAPICodePack.Shell.dll" />
<EmbeddedResource Include="$(PkgMicrosoft_WindowsAPICodePack_Core)\lib\Microsoft.WindowsAPICodePack.dll" />
<EmbeddedResource Include="$(PkgMicrosoft_WindowsAPICodePack_Shell)\lib\Microsoft.WindowsAPICodePack.Shell.dll" />
</ItemGroup>
<ItemGroup>
<Content Include="icon.ico" />
Expand Down
Binary file added Rapr/Rapr.snk
Binary file not shown.
5 changes: 4 additions & 1 deletion Rapr/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
<value>Normal</value>
</setting>
<setting name="DriverStoreViewState" serializeAs="String">
<value/>
<value />
</setting>
<setting name="UpgradeRequired" serializeAs="String">
<value>True</value>
</setting>
</Rapr.Properties.Settings>
</userSettings>
Expand Down

0 comments on commit 138885c

Please sign in to comment.