-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
76 lines (71 loc) · 2.55 KB
/
Copy pathProgram.cs
File metadata and controls
76 lines (71 loc) · 2.55 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
using System;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Principal;
static class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
if (!IsAdmin())
{
try
{
var psi = new ProcessStartInfo
{
FileName = Application.ExecutablePath,
UseShellExecute = true,
Verb = "runas"
};
Process.Start(psi);
return;
}
catch { }
}
EnableTls12();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
catch (Exception ex)
{
try { File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.log"), ex.ToString()); }
catch { }
MessageBox.Show("启动失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void EnableTls12()
{
try
{
// 启用 .NET Framework 的 TLS 1.2
Microsoft.Win32.Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319",
"SchUseStrongCrypto", 1, Microsoft.Win32.RegistryValueKind.DWord);
Microsoft.Win32.Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319",
"SchUseStrongCrypto", 1, Microsoft.Win32.RegistryValueKind.DWord);
// 启用 SChannel TLS 1.2(用于 HttpListener 的 HTTPS 服务)
Microsoft.Win32.Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server",
"Enabled", 1, Microsoft.Win32.RegistryValueKind.DWord);
Microsoft.Win32.Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server",
"DisabledByDefault", 0, Microsoft.Win32.RegistryValueKind.DWord);
}
catch { }
}
static bool IsAdmin()
{
try
{
var id = WindowsIdentity.GetCurrent();
var p = new WindowsPrincipal(id);
return p.IsInRole(WindowsBuiltInRole.Administrator);
}
catch { return false; }
}
}