-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (81 loc) · 3.06 KB
/
Program.cs
File metadata and controls
103 lines (81 loc) · 3.06 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
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace FastPst
{
internal static class Program
{
// 定义一个静态的 Mutex
private static Mutex mutex = new Mutex(true, "{A-UNIQUE-IDENTIFIER}");
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
string filePath = args[0];
if (Properties.Settings.Default.isChecked)
{
filePath = filePath.Replace("\\", "/");
}
Clipboard.SetText(filePath); // 复制路径到剪贴板
//MessageBox.Show("路径已复制到剪贴板:\n" + filePath, "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// 检查是否已有实例在运行
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.Run(new Form1());
// 释放 Mutex
mutex.ReleaseMutex();
}
else
{
// 如果有实例正在运行,通知用户并聚焦到现有实例
//MessageBox.Show("程序已经在运行。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//BringExistingInstanceToFront();
}
}
//[STAThread]
//static void Main(string[] args)
//{
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// if (args.Length > 0)
// {
// // 获取文件路径
// string filePath = args[0];
// Clipboard.SetText(filePath); // 复制到剪贴板
// }
// else
// {
// Application.Run(new Form1());
// }
//}
// 将已有实例置于最前面
private static void BringExistingInstanceToFront()
{
// 获取当前运行中的进程
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
foreach (var process in System.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName))
{
if (process.Id != currentProcess.Id)
{
// 显示并激活窗口
IntPtr handle = process.MainWindowHandle;
if (IsIconic(handle)) ShowWindow(handle, SW_RESTORE);
SetForegroundWindow(handle);
break;
}
}
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
}
}