-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (96 loc) · 3.68 KB
/
Program.cs
File metadata and controls
107 lines (96 loc) · 3.68 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PathTool
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// args = new string[]{ "-execute" };
try
{
if (args.Length > 0)
{
string currentPath = System.Environment.CurrentDirectory;
string path = Environment.GetEnvironmentVariable("path");
// MessageBox.Show("当前运行目录为 " + currentPath);
// MessageBox.Show("path: " + path);
string[] pathsRaw = path.Split(new char[] { ';' }, StringSplitOptions.None);
List<string> pathsList = new List<string>(pathsRaw);
List<string>.Enumerator en = pathsList.GetEnumerator();
bool has = false;
while (en.MoveNext())
{
string value = en.Current;
if (currentPath == value)
{
has = true;
break;
}
}
if (has)
{
DialogResult result = MessageBox.Show("当前目录\n" + currentPath + "\n已经存在于path中, 是否移除?", "是否移除", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) // 确定 移除
{
pathsList.Remove(currentPath);
Environment.SetEnvironmentVariable("path", combine(pathsList), EnvironmentVariableTarget.Machine);
}
else // 取消 退出
{
Application.Exit();
}
}
else
{
DialogResult result = MessageBox.Show("当前目录\n" + currentPath + "\n不存在于path中, 是否添加?", "是否添加", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) // 确定 添加
{
pathsList.Add(currentPath);
Environment.SetEnvironmentVariable("path", combine(pathsList), EnvironmentVariableTarget.Machine);
}
else // 取消 退出
{
Application.Exit();
}
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
catch(Exception e)
{
MessageBox.Show(e.Message,"错误");
}
}
public static string combine (List<string> strs)
{
StringBuilder content = new StringBuilder();
for(var i=0;i<strs.Count-1; i++)
{
string str = strs[i];
content.Append(str).Append(';');
}
if(strs.Count > 0)
{
content.Append(strs[strs.Count - 1]);
}
return content.ToString();
}
}
}