-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
240 lines (206 loc) · 8.51 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
/*
* File Killer
* Permanent deletion of files.
* © by Thiseas 21/11/2009 version 1.0
* 08/03/2010 version 1.5
* The program is distributed under the terms of the GNU General Public License
* Developed in C# 2008
*
* -------------------------------------------------------------------------------------------------
* Version 1.5 Updates
* 1. Overcome the 2Gb file size barrier. Now any file size can be deleted.
* 2. Before rename the file check if the new filename already exist.
* In case that exists, choose another name.
* 3. Some fixes/adjustments in the user interface.
* 4. Fix the code to handle all exceptions, not only IOExceptions.
**********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace FileKiller
{
public partial class Form1 : Form
{
public const int iBufferLength = 1024000; // Declare 1Mb buffer.
public Form1()
{
InitializeComponent();
}
private void button_Quit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button_Select_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
insertSelectedFilesIntoArray();
}
}
private void insertSelectedFilesIntoArray()
{
string filename;
bool bFileAlreadyExistInGrid = false;
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
bFileAlreadyExistInGrid = false;
filename = openFileDialog1.FileNames[i];
// Insert it into the Grid if not exist.
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
if (filename == (string)dataGridView1.Rows[j].Cells[0].Value)
{
bFileAlreadyExistInGrid = true;
break;
}
}
if (bFileAlreadyExistInGrid)
continue;
dataGridView1.Rows.Add(openFileDialog1.FileNames[i]);
}
updateStatusLine();
}
private void button_ClearGrid_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
updateStatusLine();
}
private void Button_KillFiles_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("Не указаны файлы для удаления. Таблица пуста.","Ой!");
}
else if (MessageBox.Show("Вы уверены, что хотите навсегда удалить файлы, которые указаны в таблице?",
"Удалить навсегда!",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
killTheFiles();
}
}
private void killTheFiles()
{
int i = 0;
while (i < dataGridView1.Rows.Count)
{
string filename = (string)dataGridView1.Rows[i].Cells[0].Value;
if (killFile(filename))
dataGridView1.Rows.RemoveAt(i);
else
i++;
}
}
private void updateStatusLine()
{
label_NumOfFiles.Text = dataGridView1.Rows.Count.ToString();
}
private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
updateStatusLine();
}
private bool killFile(string filename)
{
bool ret = true;
try
{
byte[] rowDataBuffer;
// I open a stream w/o the fileshare flag defined. This declines sharing of the current file.
// Any request to open the file (by this process or another process) will fail until
// the file is closed.
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
{
Cursor.Current = Cursors.WaitCursor;
FileInfo f = new FileInfo(filename);
long count = f.Length;
long offset = 0;
rowDataBuffer = new byte[iBufferLength];
while (count >= 0)
{
int iNumOfDataRead = stream.Read(rowDataBuffer, 0, iBufferLength);
// I have inside the rowDataBuffer array the contents of a fragment of the file.
if (iNumOfDataRead == 0)
{
break;
}
// I will apply the transformations to the rowDataBuffer array and then i will
// write it back to the file.
if (radioButton_RandomData.Checked)
{
Random randombyte = new Random();
randombyte.NextBytes(rowDataBuffer);
}
else if (radioButton_Blanks.Checked)
{
for (int i = 0; i < iNumOfDataRead; i++)
rowDataBuffer[i] = 0;
}
else
{
for (int i = 0; i < iNumOfDataRead; i++)
rowDataBuffer[i] = Convert.ToByte(Convert.ToChar(Convert.ToInt32(numericUpDown2.Value)));
}
// Write the new contents back to the file.
for (int i = 0; i < numericUpDown1.Value; i++)
{
stream.Seek(offset, SeekOrigin.Begin);
stream.Write(rowDataBuffer, 0, iNumOfDataRead);
}
// Calculate the new offset & count
offset += iNumOfDataRead;
count -= iNumOfDataRead;
} //while
} // using
// Substitude every filename character with random numbers from 0 to 9.
string newName = "";
do
{
Random random = new Random();
string cleanName = Path.GetFileName(filename);
string dirName = Path.GetDirectoryName(filename);
int iMoreRandomLetters = random.Next(9);
// for more security, don't use only the size of the original filename but add some random letter.
for (int i = 0; i < cleanName.Length + iMoreRandomLetters; i++)
{
newName += random.Next(9).ToString();
}
newName = dirName + "\\" + newName;
} while (File.Exists(newName));
//Rename the file to the new random name.
File.Move(filename, newName);
//Delete the file now.
File.Delete(newName);
}
catch
{
ret = false;
}
finally
{
Cursor.Current = Cursors.Default;
}
return ret;
}
private void button1_Click(object sender, EventArgs e)
{
About1 about = new About1();
about.ShowDialog();
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
label_letter.Text = Convert.ToChar(Convert.ToInt32(numericUpDown2.Value)).ToString();
}
private void radioButton_ASCII_CheckedChanged(object sender, EventArgs e)
{
numericUpDown2.Enabled = radioButton_ASCII.Checked;
label_letter.Enabled = radioButton_ASCII.Checked;
}
}
}