Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (86 loc) · 3.15 KB
/
Copy pathProgram.cs
File metadata and controls
95 lines (86 loc) · 3.15 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
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace LinView
{
internal static class Program
{
private static Mutex? _singleInstanceMutex;
[STAThread]
static void Main(string[] args)
{
// 提权子进程:只写注册表
if (args.Length >= 1 && args[0] == "--associate")
{
bool isRemove = args.Length >= 2 && args[1] == "--remove";
var extensions = new string[Math.Max(0, args.Length - 2)];
if (extensions.Length > 0)
Array.Copy(args, 2, extensions, 0, extensions.Length);
try
{
foreach (var ext in extensions)
{
if (isRemove)
FileAssociationHelper.Unassociate(ext);
else
FileAssociationHelper.Associate(ext);
}
Environment.Exit(0);
}
catch (Exception ex)
{
MessageBox.Show($"写入注册表失败:{ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
return;
}
var settings = AppSettings.Load();
// ===== 解析命令行参数 =====
string? fileToOpen = null;
string? masonryDir = null;
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--masonry" && i + 1 < args.Length)
{
masonryDir = args[i + 1];
i++;
}
else if (!args[i].StartsWith("--") && File.Exists(args[i]))
{
fileToOpen = args[i];
}
}
// ===== 单实例模式 =====
if (!settings.AllowMultipleInstances)
{
_singleInstanceMutex = new Mutex(true, AppConstants.SingleInstanceMutexName, out bool createdNew);
if (!createdNew)
{
// 已有实例在运行:把请求发给它,然后自己退出
try
{
if (!string.IsNullOrEmpty(masonryDir))
{
SingleInstancePipe.SendToExistingInstance("MZ", masonryDir);
}
else if (!string.IsNullOrEmpty(fileToOpen))
{
SingleInstancePipe.SendToExistingInstance("IMG", fileToOpen);
}
}
finally
{
_singleInstanceMutex.Dispose();
_singleInstanceMutex = null;
}
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(fileToOpen, masonryDir));
}
}
}