Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
174 lines (149 loc) · 6.14 KB
/
Copy pathProgram.cs
File metadata and controls
174 lines (149 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security;
using System.Windows.Forms;
using Bluegrams.Application;
using Rapr.Lang;
using Rapr.Utils;
namespace Rapr
{
public static class Program
{
static Program()
{
AppDomain.CurrentDomain.AssemblyResolve += ResolveEventHandler;
}
private static Assembly ResolveEventHandler(object sender, ResolveEventArgs args)
{
Assembly assembly = Assembly.GetEntryAssembly();
string resourceName = $"{assembly.EntryPoint.DeclaringType.Namespace}.{new AssemblyName(args.Name).Name}.dll";
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
return null;
}
byte[] assemblyData = new byte[stream.Length];
_ = stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
private static void AddEnvironmentPaths(string path)
{
var paths = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty, path };
string newPath = string.Join(Path.PathSeparator.ToString(), paths);
Environment.SetEnvironmentVariable("PATH", newPath);
}
private static void CleanUpOldConfig()
{
FileInfo fileInfo = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);
DirectoryInfo strongAssemblyConfigDirectory = fileInfo.Directory.Parent;
if (strongAssemblyConfigDirectory.Exists)
{
// 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;
if (configDirectory.Exists)
{
// 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>
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Trace.AutoFlush = true;
Trace.IndentSize = 4;
Trace.Listeners.Add(new TextFileTraceListener());
AddEnvironmentPaths(@"C:\Windows\System32\CompatTel");
UpdateManager.CleanUpOldFiles();
try
{
string settingFile = $"{Application.ProductName}.user.config";
string applicationFolderPath = DSEFormHelper.GetApplicationFolder();
try
{
// Test if we can open filePath as Read/Write
using (FileStream fs = new FileStream(Path.Combine(applicationFolderPath, settingFile), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
}
PortableSettingsProvider.SettingsFileName = settingFile;
PortableSettingsProvider.SettingsDirectory = applicationFolderPath;
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);
}
catch (SecurityException)
{
}
catch (UnauthorizedAccessException)
{
}
catch (IOException)
{
}
if (Properties.Settings.Default.UpgradeRequired)
{
try
{
// Upgrade settings from previous version
Properties.Settings.Default.Upgrade();
DriverStoreFactory.MigrateDriverStoreSettings();
// Mark upgrade as completed
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
Trace.TraceInformation("Settings migration completed successfully");
}
catch (Exception ex)
{
// Log the error but don't fail the application startup
Trace.TraceError($"Settings migration failed: {ex}");
// Still mark upgrade as completed to avoid repeated attempts
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
}
try
{
CleanUpOldConfig();
}
catch (Exception e) when (
e is SecurityException
|| e is IOException)
{
Trace.TraceError(e.ToString());
}
}
DriverStoreFactory.ValidateDriverStoreOption();
using (DSEForm mainForm = new DSEForm())
{
Application.Run(mainForm);
}
}
catch (ConfigurationException e)
{
Trace.TraceError(e.ToString());
MessageBox.Show(
e.Message,
Language.Product_Name,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}