-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm1.cs
438 lines (366 loc) · 15.9 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//拡張子変更用
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.Runtime.InteropServices;
//Webp用
//using ImageProcessor;
using ImageProcessor.Plugins.WebP.Imaging.Formats;
namespace ext_chg_detect
{
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
this.WindowState = FormWindowState.Minimized;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
NotifyIcon notifyIcon;
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("favicon.ico");
notifyIcon.Visible = true;
notifyIcon.Text = "拡張子変更ディテクター";
// コンテキストメニュー
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
toolStripMenuItem.Text = "&終了";
toolStripMenuItem.Click += ToolStripMenuItem_Click;
contextMenuStrip.Items.Add(toolStripMenuItem);
notifyIcon.ContextMenuStrip = contextMenuStrip;
if (GetValueString("basic", "path", "data\\info.ini") == "")
{
DialogResult result = MessageBox.Show("検知するフォルダーを設定しますか?", "未登録", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
BrowseFolder();
DialogResult result_startup = MessageBox.Show("フォルダの設定が完了しました。このアプリをコンピュータの起動時に自動的に起動するよう設定しますか?", "スタートアップ", MessageBoxButtons.YesNo);
if (result_startup == System.Windows.Forms.DialogResult.Yes)
{
SetValueString("basic", "startup", "true", "data\\info.ini");
RegisterStartupApp("Ext-Change-Detect", Application.ExecutablePath);
MessageBox.Show("スタートアップアプリに登録しました。このアプリはバックグラウンドで実行されるため、ウィンドウなどは表示されませんのでご注意ください。自動起動設定と、検知するフォルダーの設定は後から変更できます。");
}
else
{
}
}
else
{
Application.Exit();
}
}
else
{
}
if (GetValueString("basic", "startup", "data\\info.ini") == "true")
{
RegisterStartupApp("Ext-Change-Detect", Application.ExecutablePath);
}
else
{
UnregisterStartupApp("Ext-Change-Detect");
}
label1.Text = GetValueString("basic", "path", "data\\info.ini");
//監視対象のフォルダの設定
fsw.Path = GetValueString("basic", "path", "data\\info.ini");
//監視する種類の設定
fsw.NotifyFilter =
(NotifyFilters.Attributes
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName);
//サブディレクトリも監視
fsw.IncludeSubdirectories = true;
//すべてのファイルを監視しているならば
fsw.Filter = "";
//WindowFormなどUI用
//コンソールではいらない
fsw.SynchronizingObject = this;
//イベント設定
fsw.Renamed += fsw_Renamed;
//監視を開始
fsw.EnableRaisingEvents = true;
}
void BrowseFolder()
{
using (var ofd = new OpenFileDialog() { FileName = "SelectFolder", Filter = "Folder|.", CheckFileExists = false })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
SetValueString("basic", "path", Path.GetDirectoryName(ofd.FileName), "data\\info.ini");
}
}
}
FileSystemWatcher fsw = new FileSystemWatcher();
private void FileWatcher_Load(object sender, EventArgs e)
{
}
void fsw_Renamed(object sender, RenamedEventArgs e)
{
//イベントの発生を一時的に停止
fsw.EnableRaisingEvents = false;
//MessageBox.Show(e.OldName);
string Old_ext = System.IO.Path.GetExtension(e.OldName);
string New_ext = System.IO.Path.GetExtension(e.Name);
label1.Text = Old_ext;
ChangeFileExtension(e.FullPath, Old_ext);
//拡張子が変わっている場合のみ処理を行う
if (Old_ext != New_ext)
{
if (New_ext == ".png" && Old_ext == ".jpg")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "png", "jpg", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".jpg" && Old_ext == ".png")
{
if (!File.Exists("tmp\\" + e.OldName)) {
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "jpg", "png", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".jpeg" && Old_ext == ".png")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "jpg", "png", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".jpg" && Old_ext == ".webp")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "jpg", "webp", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".jpeg" && Old_ext == ".webp")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "jpg", "webp", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".png" && Old_ext == ".webp")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "png", "webp", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".webp" && Old_ext == ".jpg")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "webp", "jpg", e.OldName);
File.Delete(e.OldFullPath);
}
else if (New_ext == ".webp" && Old_ext == ".png")
{
if (!File.Exists("tmp\\" + e.OldName))
{
File.Copy(e.OldFullPath, "tmp\\" + e.OldName);
}
changeFormatOfPicture(e.OldFullPath, e.FullPath, "webp", "png", e.OldName);
File.Delete(e.OldFullPath);
}
}
//イベントの発生を再開
fsw.EnableRaisingEvents = true;
}
void changeFormatOfPicture(string fromFilePath, string toFilePath, string toExt, string fromExt, string OldName)
{
//File.Copy(fromFilePath, "tmp\\" + OldName);
if (fromExt == "png")
{
if (toExt == "jpg")
{
using (Image imageData = Image.FromFile("tmp\\" + OldName))
{
// 新しいビットマップを作成する
Bitmap bmp = new Bitmap(imageData.Width, imageData.Height);
// Graphicsオブジェクトを作成する
Graphics g = Graphics.FromImage(bmp);
// 背景を白色で塗りつぶす
g.Clear(Color.White);
// 元の画像を描画する
g.DrawImageUnscaled(imageData, 0, 0);
// Graphicsオブジェクトを破棄する
g.Dispose();
// ビットマップをJPEG形式で保存する
bmp.Save(toFilePath, ImageFormat.Jpeg);
// ビットマップを破棄する
bmp.Dispose();
}
}
else if(toExt == "webp"){
var wf = new WebPFormat();
using (var image = new Bitmap("tmp\\" + OldName))
{
wf.Save(toFilePath, image, 0);
}
}
}
else if (fromExt == "jpg")
{
if (toExt == "png")
{
using (Image imageData = Image.FromFile("tmp\\" + OldName))
{
// ビットマップをJPEG形式で保存する
imageData.Save(toFilePath, ImageFormat.Png);
// ビットマップを破棄する
imageData.Dispose();
}
}
else if (toExt == "webp")
{
var wf = new WebPFormat();
using (var image = new Bitmap("tmp\\" + OldName))
{
wf.Save(toFilePath, image, 0);
}
}
}
else if (fromExt == "webp")
{
var wf = new WebPFormat();
if (toExt == "jpg")
{
using (var image = (Bitmap)wf.Load(new FileStream("tmp\\" + OldName, FileMode.Open, FileAccess.Read)))
{
image.Save(toFilePath, ImageFormat.Jpeg);
}
}
else if (toExt == "png")
{
using (var image = (Bitmap)wf.Load(new FileStream("tmp\\" + OldName, FileMode.Open, FileAccess.Read)))
{
image.Save(toFilePath, ImageFormat.Png);
}
}
}
}
// ファイル名と新しい拡張子を引数にとる関数
public static void ChangeFileExtension(string fileName, string newExtension)
{
try
{
// ファイルが存在するかどうかをチェックする
if (File.Exists(fileName))
{
// ファイルのパスと名前を分離する
string filePath = Path.GetDirectoryName(fileName);
string fileBaseName = Path.GetFileNameWithoutExtension(fileName);
// 新しいファイル名を作る
string newFileName = Path.Combine(filePath, fileBaseName + newExtension);
// ファイル名を変更する
File.Move(fileName, newFileName);
}
else
{
// ファイルが存在しない場合はエラーメッセージを表示する
Console.WriteLine("ファイルが見つかりませんでした。");
}
}
catch { }
}
public static void RegisterStartupApp(string appName, string appPath)
{
// レジストリのキーを開く
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
// アプリケーション名と実行可能パスを値として設定する
rkApp.SetValue(appName, appPath);
}
// アプリケーション名を引数にとり、レジストリからスタートアップアプリを解除する関数
public static void UnregisterStartupApp(string appName)
{
// レジストリのキーを開く
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
// アプリケーション名に対応する値が存在するかチェックする
if (rkApp.GetValue(appName) != null)
{
// 値が存在する場合は削除する
rkApp.DeleteValue(appName, false);
}
}
//---DLL関数の定義 START---//
//GetPrivateProfileString関数の定義
[DllImport("KERNEL32.DLL")]
public static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
//GetPrivateProfileInt関数の定義
[DllImport("KERNEL32.DLL")]
public static extern uint GetPrivateProfileInt(
string lpAppName,
string lpKeyName,
int nDefault,
string lpFileName);
//WritePrivateProfileString関数の定義
[DllImport("KERNEL32.DLL")]
public static extern bool WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName);
//---DLL関数の定義 END ---//
// DLL関数をラップしたメソッドです。
public string GetValueString(string section, string key, string fileName)
{
var sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, "", sb, Convert.ToUInt32(sb.Capacity), fileName);
return sb.ToString();
}
public int GetValueInt(string section, string key, string fileName)
{
var sb = new StringBuilder(1024);
return (int)GetPrivateProfileInt(section, key, 0, fileName);
}
// DLL関数をラップしたメソッドです。
public bool SetValueString(string section, string key, string value, string fileName)
{
return WritePrivateProfileString(section, key, value, fileName);
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
// アプリケーションの終了
Application.Restart();
Environment.Exit(0);
}
}
}