-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
265 lines (239 loc) · 9.61 KB
/
Form1.cs
File metadata and controls
265 lines (239 loc) · 9.61 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
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
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Rebuild_Icon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
string url = folder_url.Text;
string bas = folder_icon.Text;
string type = TypeIcon.Text;
if (Directory.Exists(url))
{
log($"~ Found Folder {url} and Set to {type}");
int all = 0;
int found = 0;
int error = 0;
int remove_icon = 0;
int noremove_icon = 0;
int remove_config = 0;
DirectoryInfo info = new DirectoryInfo(url);
foreach (DirectoryInfo file in info.GetDirectories())
{
var dir = file.FullName;
var ico = @$"{dir}\{bas}";
var ini = @$"{dir}\desktop.ini";
var hid = @$"{dir}\.hidden";
all++;
//log(ico);
//Thread.Sleep(600);
if (is_remove_config.Checked)
{
var tes = RetIcon(dir, bas,is_remove_icon_found.Checked);
if (!tes)
{
log($"Faild - {ico} : Remove Config False - Remove Icon {is_remove_icon_found.Checked}");
}
else
{
if (is_remove_icon_found.Checked)
{
log($"Remove - {ico} : Remove Config {tes} - Remove Icon True");
}
}
remove_config++;
}
else
{
// check file icon
if (File.Exists(ico))
{
try
{
// Making file ini
string[] lines = { "[.ShellClassInfo]", $"IconResource={bas},0", "[ViewState]", "Mode=", "Vid=", $"FolderType={type}" };
File.WriteAllLines(ini, lines);
// Making file hide
string[] linesLinux = { "desktop.ini", bas };
File.WriteAllLines(dir + @"\.hidden", linesLinux);
// Making system files and hidden
File.SetAttributes(ini, File.GetAttributes(ini) | FileAttributes.Hidden | FileAttributes.System);
File.SetAttributes(ico, File.GetAttributes(ico) | FileAttributes.Hidden);
File.SetAttributes(hid, File.GetAttributes(hid) | FileAttributes.Hidden | FileAttributes.System);
// Making folder Read Only?
//File.SetAttributes(dir, File.GetAttributes(dir) | FileAttributes.ReadOnly);
//var tes = RandomNumber(0, 999);
// Attempt 01
//Directory.Move(dir, dir + $"_Processing_{tes}");
//Directory.Move(dir + $"_Processing_{tes}", dir);
found++;
}
catch (Exception ee)
{
log($"Error - {ico} : {ee.Message}");
error++;
}
}
else
{
log($"{ico} : No Icon Found?");
error++;
}
}
}
// Reset Icon
if (auto_clear.Checked)
{
RefreshIcons();
}
log($"All Folder {all} | Icon OK {found} - Icon Error {error} | Remove Config {remove_config}");
}
else
{
log("~ No Found Folder");
}
}
public void log(string send)
{
Log.AppendText(send);
Log.AppendText(Environment.NewLine);
}
public bool RefreshIcons()
{
// Attempt 02
string localIconCachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\IconCache.db";
if (File.Exists(localIconCachePath))
{
File.Delete(localIconCachePath);
}
// Attempt 03
string dirCachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\Windows\Explorer\";
DirectoryInfo di = new DirectoryInfo(dirCachePath);
FileInfo[] files = di.GetFiles("iconcache*.db");
foreach (FileInfo file in files)
{
File.Delete(file.FullName);
}
// Attempt 04.01
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ie4uinit.exe -ClearIconCache";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
process.Start();
}
// Attempt 04.02
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ie4uinit.exe -ClearIconCache";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
process.Start();
}
// Attempt 05
/*
foreach (Process p in Process.GetProcesses())
{
if (p.MainModule.ModuleName.Contains("explorer") == true)
{
p.Kill();
}
}
Process.Start("explorer.exe");
*/
// Attempt 06
// Copied from stackoverflow.com
SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);//SHCNE_ASSOCCHANGED SHCNF_IDLIST
return true;
}
public void Restart()
{
// find all the explorer processes and kill them
Process[] explorer = Process.GetProcessesByName("explorer");
foreach (Process process in explorer)
{
process.Kill();
}
// start a new explorer process
Process.Start("explorer.exe");
}
public bool RetIcon(string dir, string icon,bool remove=false)
{
try
{
// desktop.ini
if (File.Exists(dir + @"\desktop.ini"))
{
File.SetAttributes(dir + @"\desktop.ini", File.GetAttributes(dir + @"\desktop.ini") | FileAttributes.Normal); //Normal file
FileInfo fileInfo = new FileInfo(dir + @"\desktop.ini")
{
IsReadOnly = false
};
File.Delete(dir + @"\desktop.ini");
}
// .hidden
if (File.Exists(dir + @"\.hidden"))
{
File.SetAttributes(dir + @"\.hidden", File.GetAttributes(dir + @"\.hidden") | FileAttributes.Normal); //Normal file
FileInfo fileInfo = new FileInfo(dir + @"\.hidden")
{
IsReadOnly = false
};
File.Delete(dir + @"\.hidden");
}
// Icon.ico
if (File.Exists(dir + @"\" + icon))
{
File.SetAttributes(dir + @"\" + icon, File.GetAttributes(dir + @"\" + icon) | FileAttributes.Normal); //Normal file
FileInfo fileInfo = new FileInfo(dir + @"\" + icon)
{
IsReadOnly = false
};
if (remove)
{
File.Delete(dir + @"\" + icon);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
// Instantiate random number generator.
private readonly Random _random = new Random();
// Generates a random number within a range.
public int RandomNumber(int min, int max)
{
return _random.Next(min, max);
}
// Copied from stackoverflow.com
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern void SHChangeNotify(int wEventId, int uFlags, IntPtr dwItem1, IntPtr dwItem2);
private void Form1_Load(object sender, EventArgs e)
{
TypeIcon.SelectedIndex = 0;
}
private void bt_clear_cache_Click(object sender, EventArgs e)
{
RefreshIcons();
}
private void bt_restart_Click(object sender, EventArgs e)
{
Restart();
}
}
}