-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainWindow.xaml.cs
173 lines (147 loc) · 5.36 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace FileIconMOD
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string fileType = TextBox_FileType.Text;
string iconPath = TextBox_IconPath.Text;
string openProgram = TextBox_OpenExe.Text;
bool isClean = CheckBox_CleanCfg.IsChecked == true;
if (!CheckInput(ref fileType, ref iconPath, ref openProgram))
{
return;
}
StringBuilder argBuilder = new StringBuilder();
if (fileType != null)
argBuilder.Append($"-type \"{fileType}\" ");
if (iconPath != null)
argBuilder.Append($"-icon \"{iconPath}\" ");
if (openProgram != null)
argBuilder.Append($"-open \"{openProgram}\" ");
if (isClean)
argBuilder.Append($"-clean");
System.Diagnostics.Process.Start("FileIconConsole.exe", argBuilder.ToString());
}
private bool CheckInput(ref string fileType, ref string iconPath, ref string openProgram)
{
if (fileType == "" || fileType == null)
{
MessageBox.Show("请输入需要修改图标的类型\n例如 .xlsx或将文件拖入 ");
return false;
}
else if (fileType.ToLower() == ".lnk" || fileType.ToLower() == "lnk"
|| fileType.ToLower() == ".exe" || fileType.ToLower() == "exe")
{
MessageBox.Show("本工具不支持修改.lnk和.exe文件的图标");
return false;
}
else
{
if (fileType.IndexOf(".") < 0)
{
fileType = "." + fileType;
}
}
if (CheckBox_IconPath.IsChecked == true)
{
if (string.IsNullOrWhiteSpace(iconPath))
{
iconPath = "";
}
else if (!File.Exists(iconPath))
{
MessageBox.Show("图标路径有误\n提示:可以输入图标路径或将图标文件拖入");
return false;
}
else
{
var iconFile = new FileInfo(iconPath);
var iconExt = iconFile.Extension.ToLower();
if (iconExt != ".bmp" && iconExt != ".jpg" && iconExt != ".jpeg" && iconExt != ".png")
{
MessageBox.Show("图标格式不支持\n提示:当前支持.bmp .jpg .jpeg .png格式的图标");
return false;
}
}
}
else
{
iconPath = null;
}
if (CheckBox_OpenExe.IsChecked == true)
{
if (string.IsNullOrWhiteSpace(openProgram))
{
openProgram = "";
}
else if (!File.Exists(openProgram))
{
MessageBox.Show("程序路径错误\n提示:可以输入程序路径或将程序文件拖入");
return false;
}
}
else
{
openProgram = null;
}
return true;
}
private void Text_FilePath_PreviewDragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
private void Text_FilePath_PreviewDrop(object sender, DragEventArgs e)
{
var txb = ((TextBox)sender);
txb.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
txb.Focus();
}
private void TextBox_FilePath_TextChanged(object sender, TextChangedEventArgs e)
{
var txb = ((TextBox)sender);
}
private void Text_FileType_PreviewDragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
private void Text_FileType_PreviewDrop(object sender, DragEventArgs e)
{
var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
if (File.Exists(path))
{
var file = new FileInfo(path);
var ext = file.Extension.ToLower();
((TextBox)sender).Text = ext;
}
}
private void TextBox_FileType_LostFocus(object sender, RoutedEventArgs e)
{
var txb = ((TextBox)sender);
txb.SelectionStart = txb.Text.Length;
}
}
}