-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
475 lines (428 loc) · 18.4 KB
/
Form1.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FileEncodingTransform
{
public partial class Form1 : Form
{
private Button btnRemove;
private Button btnSelectFiles;
private Button button_Clipboard;
private Button buttonBrowser;
private Button buttonRun;
private CheckBox chkIsBackup;
private CheckBox chkUnknownEncoding;
private ComboBox cmbSourceEncode;
private ComboBox cmbTargetEncode;
private IContainer components = null;
private ArrayList ExtNameList;
private FolderBrowserDialog folderBrowserDialog1;
private string initPth = @"E:\Projects";
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private TextBox textBox_fileFilter;
private TextBox txtResult;
public Form1()
{
InitializeComponent();
this.cmbSourceEncode.Enabled = !this.chkUnknownEncoding.Checked;
this.cmbTargetEncode.SelectedIndex = 0;
//progressBar1.Value = 0;
}
private void btnRemove_Click(object sender, EventArgs e)
{
//if (this.listSelectedFiles.SelectedIndex > -1)
//{
// this.listSelectedFiles.Items.RemoveAt(this.listSelectedFiles.SelectedIndex);
//}
foreach (ListViewItem lvi in listView1.SelectedItems) //选中项遍历
{
listView1.Items.RemoveAt(lvi.Index); // 按索引移除
}
}
private void btnSelectFiles_Click(object sender, EventArgs e)
{
int cfflag = 0;
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Multiselect = true;
dialog.InitialDirectory = this.initPth;
if (!this.textBox_fileFilter.Text.Trim().Equals(""))
{
dialog.Filter = this.textBox_fileFilter.Text.Trim()+fileFilter_all(this.textBox_fileFilter.Text.Trim());
}
if (DialogResult.OK == dialog.ShowDialog())
{
string[] fileNames = dialog.FileNames;
this.listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
this.Cursor = Cursors.WaitCursor;
foreach (string str in fileNames)
{
for (int nColOrder = 0; nColOrder < listView1.Items.Count; nColOrder++)//每行中的每项
{
if (str == listView1.Items[nColOrder].Text)
{
cfflag = 1;
break;
}
}
if (cfflag == 0 && File.Exists(str))
{
ListViewItem lvi = new ListViewItem();
lvi.Text = str;
FileInfo testfile = new FileInfo(str);
IdentifyEncoding encoding2 = new IdentifyEncoding();
lvi.SubItems.Add(encoding2.GetEncodingName(testfile));
lvi.SubItems.Add(cmbTargetEncode.SelectedItem.ToString());
this.listView1.Items.Add(lvi);
}
}
this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。
this.Cursor = Cursors.Default;
}
}
}
private string fileFilter_all(string fileFilter_text)
{
int x = 0;
string filefilter_str1="";
string filefilter_str2="";
foreach (var str in fileFilter_text.Split('|'))
{
if (x % 2 == 0)
{
filefilter_str1 += str + ';';
}
else {
filefilter_str2 += str + ';';
}
x++;
}
if (fileFilter_text[fileFilter_text.Length - 1] == '|')
{
return filefilter_str1.TrimEnd(';') + '|' + filefilter_str2.TrimEnd(';');
}
else
{
return '|'+ filefilter_str1.TrimEnd(';') + '|' + filefilter_str2.TrimEnd(';');
}
}
private void button_Clipboard_Click(object sender, EventArgs e)
{
string[] strArray = Clipboard.GetText().Split(new char[] { '\n' });
string item = "";
int cfflag = 0;
this.listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
this.Cursor = Cursors.WaitCursor;
for (int i = 0; i < strArray.Length; i++)
{
item = strArray[i];
if (item.IndexOf("\r") > -1)
{
item = item.Replace("\r", "");
}
if (!item.Trim().Equals(""))
{
for (int nColOrder = 0; nColOrder < listView1.Items.Count; nColOrder++)//每行中的每项
{
if (item == listView1.Items[nColOrder].Text)
{
cfflag = 1;
break;
}
}
if (cfflag == 0 && File.Exists(item))
{
ListViewItem lvi = new ListViewItem();
lvi.Text = item;
FileInfo testfile = new FileInfo(item);
IdentifyEncoding encoding2 = new IdentifyEncoding();
lvi.SubItems.Add(encoding2.GetEncodingName(testfile));
lvi.SubItems.Add(cmbTargetEncode.SelectedItem.ToString());
this.listView1.Items.Add(lvi);
}
}
}
this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。
this.Cursor = Cursors.Default;
}
private void buttonBrowser_Click(object sender, EventArgs e)
{
this.folderBrowserDialog1.SelectedPath = this.initPth;
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.initPth = this.folderBrowserDialog1.SelectedPath;
if (!this.textBox_fileFilter.Text.Trim().Equals(""))
{
string[] strArray = this.textBox_fileFilter.Text.Trim().Split(new char[] { '|' });
if ((strArray.Length % 2) > 0)
{
MessageBox.Show("文件过滤字符串设置有误!");
return;
}
this.ExtNameList = new ArrayList();
for (int i = 0; i < (strArray.Length / 2); i++)
{
this.ExtNameList.Add(strArray[(i * 2) + 1].Substring(strArray[(i * 2) + 1].LastIndexOf("."), strArray[(i * 2) + 1].Length - strArray[(i * 2) + 1].LastIndexOf(".")).ToLower());
}
}
this.Cursor = Cursors.WaitCursor;
this.FindAllFiles(this.folderBrowserDialog1.SelectedPath);
this.Cursor = Cursors.Default;
}
}
private void buttonRun_Click(object sender, EventArgs e)
{
this.txtResult.Text = "文件总数:" + this.listView1.Items.Count.ToString() + "\r\n正在执行......";
progressBar1.Maximum = listView1.Items.Count;
progressBar1.Value = 0;
int num = 0;
this.Cursor = Cursors.WaitCursor;
while (num < this.listView1.Items.Count)
{
num++;
//编码相同
if (listView1.Items[num - 1].SubItems[1].Text == listView1.Items[num - 1].SubItems[2].Text)
{
this.txtResult.Text = this.txtResult.Text + "\r\n不需要转换:" + this.listView1.Items[num - 1].Text.ToString();
continue;
}
//文件不存在
if (!File.Exists(this.listView1.Items[num - 1].Text))
{
this.txtResult.Text = this.txtResult.Text + "\r\n文件不存在:" + this.listView1.Items[num - 1].Text.ToString();
continue;
}
//不支持格式处理
if (listView1.Items[num - 1].SubItems[1].Text.ToLower() == "other") {
txtResult.Text = txtResult.Text + "\r\n文件格式不正确或已损坏:" + this.listView1.Items[num - 1].Text.ToString();
continue;
}
//处理文件,包含出错处理
try
{
ConvertFileEncode(this.listView1.Items[num - 1].Text.ToString());
}
catch (Exception exception)
{
txtResult.Text = txtResult.Text + "\r\n转换时出现错误:" + this.listView1.Items[num - 1].Text.ToString() + "错误原因:" + exception.Message;
continue;
}
this.txtResult.Text = this.txtResult.Text + "\r\n转换完成:" + this.listView1.Items[num - 1].Text.ToString();
//更新进度条
if (progressBar1.Value <= progressBar1.Maximum)
{
progressBar1.Value++;
}
}
this.Cursor = Cursors.Default;
this.listView1.BeginUpdate();
for (int nColOrder = 0; nColOrder < listView1.Items.Count; nColOrder++)//每行中的每项
{
//更新现有编码
string str2 = listView1.Items[nColOrder].Text;
//Console.WriteLine(str2);
if (File.Exists(str2))
{
FileInfo testfile = new FileInfo(str2.ToString());
IdentifyEncoding encoding2 = new IdentifyEncoding();
listView1.Items[nColOrder].SubItems[1].Text = encoding2.GetEncodingName(testfile);
}
}
this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。
}
private void chkUnknownEncoding_CheckedChanged(object sender, EventArgs e)
{
this.cmbSourceEncode.Enabled = !this.chkUnknownEncoding.Checked;
}
private void ConvertFileEncode(string PathFile)
{
if (!PathFile.Trim().Equals(""))
{
//if (!File.Exists(PathFile))
//{
// this.txtResult.Text = this.txtResult.Text + string.Format("\r\n{0}文件不存在。 ", PathFile);
// return;
//}
Encoding selectEncoding;
if (this.chkUnknownEncoding.Checked)
{
IdentifyEncoding encoding2 = new IdentifyEncoding();
FileInfo testfile = new FileInfo(PathFile);
string name = string.Empty;
name = encoding2.GetEncodingName(testfile);
testfile = null;
if (name.ToLower() == "other")
{
//this.txtResult.Text = this.txtResult.Text + string.Format("\r\n{0}文件格式不正确或已损坏。 ", PathFile);
return;
}
if (name != "UTF-8(BOM)")
{
selectEncoding = Encoding.GetEncoding(name);
}
else
{
selectEncoding = Encoding.UTF8;
}
}
else
{
selectEncoding = this.GetSelectEncoding(this.cmbSourceEncode.SelectedIndex);
}
string contents = File.ReadAllText(PathFile, selectEncoding);
if (this.chkIsBackup.Checked)
{
File.WriteAllText(PathFile + ".bak", contents, selectEncoding);
}
File.WriteAllText(PathFile, contents, this.GetSelectEncoding(this.cmbTargetEncode.SelectedIndex));
}
}
private void FindAllFiles(string path)
{
string[] files = Directory.GetFiles(path);
string str = "";
int startIndex = 0;
bool flag = false;
int cfflag = 0;
foreach (string str2 in this.ExtNameList)
{
if (str2.Equals(".*"))
{
flag = true;
}
}
this.listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
foreach (string str2 in files)
{
startIndex = str2.LastIndexOf(".");
if (startIndex > -1)
{
str = str2.Substring(startIndex, str2.Length - startIndex);
}
else
{
str = "";
}
if (((this.ExtNameList == null) || (this.ExtNameList.IndexOf(str.ToLower()) > -1)) || flag)
{
//this.listSelectedFiles.Items.Add(str2);
for (int nColOrder = 0; nColOrder < listView1.Items.Count; nColOrder++)//每行中的每项
{
if (str2 == listView1.Items[nColOrder].Text)
{
cfflag = 1;
break;
}
}
if (cfflag == 0 && File.Exists(str2))
{
ListViewItem lvi = new ListViewItem();
lvi.Text = str2;
FileInfo testfile = new FileInfo(str2);
IdentifyEncoding encoding2 = new IdentifyEncoding();
lvi.SubItems.Add(encoding2.GetEncodingName(testfile));
lvi.SubItems.Add(cmbTargetEncode.SelectedItem.ToString());
this.listView1.Items.Add(lvi);
}
}
}
this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。
string[] directories = Directory.GetDirectories(path);
foreach (string str2 in directories)
{
this.FindAllFiles(str2);
}
}
private void FormFileEncode_Load(object sender, EventArgs e)
{
this.cmbSourceEncode.SelectedIndex = 4;
this.cmbTargetEncode.SelectedIndex = 0;
}
private Encoding GetSelectEncoding(int i)
{
switch (i)
{
case 0:
return new UTF8Encoding(false);
case 1:
return Encoding.UTF8;
case 2:
return Encoding.UTF7;
case 3:
return Encoding.Unicode;
case 4:
return Encoding.ASCII;
case 5:
return Encoding.GetEncoding(0x3a8);
case 6:
return Encoding.GetEncoding("BIG5");
}
return new UTF8Encoding(false);
}
private void buttonclear_Click(object sender, EventArgs e)
{
this.listView1.Items.Clear(); //只移除所有的项。
}
private void cmbTargetEncode_SelectedIndexChanged(object sender, EventArgs e)
{
this.listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
for (int nColOrder = 0; nColOrder < listView1.Items.Count; nColOrder++)//每行中的每项
{
string str2 = listView1.Items[nColOrder].Text;
//Console.WriteLine(str2);
//FileInfo testfile = new FileInfo(str2.ToString());
//IdentifyEncoding encoding2 = new IdentifyEncoding();
listView1.Items[nColOrder].SubItems[2].Text = cmbTargetEncode.SelectedItem.ToString();
}
this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
int cfflag = 0;
string[] ss = e.Data.GetData(DataFormats.FileDrop, false) as string[];
this.listView1.BeginUpdate();
this.Cursor = Cursors.WaitCursor;
foreach (string s in ss)
{
for (int nColOrder = 0; nColOrder < listView1.Items.Count; nColOrder++)//每行中的每项
{
if (s == listView1.Items[nColOrder].Text)
{
cfflag = 1;
break;
}
}
if (cfflag == 0 && File.Exists(s))
{
ListViewItem lvi = new ListViewItem();
lvi.Text = s;
FileInfo testfile = new FileInfo(s);
IdentifyEncoding encoding2 = new IdentifyEncoding();
lvi.SubItems.Add(encoding2.GetEncodingName(testfile));
lvi.SubItems.Add(cmbTargetEncode.SelectedItem.ToString());
this.listView1.Items.Add(lvi);
}
}
this.listView1.EndUpdate();
this.Cursor = Cursors.Default;
}
//拖拽文件
private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All; //重要代码:表明是所有类型的数据,比如文件路径
else
e.Effect = DragDropEffects.None;
}
}
}