Repository navigation
Expand file tree
/
Copy pathForm1.cs
More file actions
140 lines (122 loc) · 4.04 KB
/
Copy pathForm1.cs
File metadata and controls
140 lines (122 loc) · 4.04 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileStats
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnFile_Click(object sender, EventArgs e)
{
FileInfo fi = null;
listView1.View = View.Details;
listView1.Columns.Add("Byte Value");
listView1.Columns.Add("Count");
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Select a file";
if (dialog.ShowDialog() == DialogResult.OK)
{
fi = new FileInfo(dialog.FileName);
}
if (fi != null)
{
if (fi.Length > 0)
{
txtFileName.Text = fi.FullName;
txtFileSize.Text = Convert.ToString(fi.Length);
FileRead(fi);
}
else
{
MessageBox.Show("File size should be at least one byte of size", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
}
private void FileRead(FileInfo fi)
{
Thread oThread = new Thread(() => FileProcess(fi));
oThread.Start();
}
private void FileProcess(FileInfo fi)
{
int byteCapacity = 2048;
FileStream fs = new FileStream(fi.FullName, FileMode.Open, System.IO.FileAccess.Read);
byte[] chunk = new byte[byteCapacity];
Queue<byte> fQueue = new Queue<byte>();
while (true)
{
int index = 0;
while (index < chunk.Length)
{
int bytesRead = fs.Read(chunk, index, chunk.Length - index);
if (bytesRead == 0)
break;
index += bytesRead;
}
if (index != 0)
{
byte[] cbuffer = new byte[index];
fs.Lock(0, byteCapacity);
for (int i = 0; i < index; i++)
{
fQueue.Enqueue(chunk[i]);
}
fs.Unlock(0, byteCapacity);
}
if (index != byteCapacity)
{
fs.Flush();
fs.Close();
ListByteCount(fQueue);
return;
}
}
}
private void ListByteCount(Queue<byte> fQueue)
{
Dictionary<string, int> byteList = new Dictionary<string, int>();
byte[] byteArray = new byte[fQueue.Count];
for (int i = 0; i < byteArray.Length; i++)
{
string byt = fQueue.Dequeue().ToString("X2");
if (byteList.ContainsKey(byt))
{
byteList[byt] = GetCount(byteList, byt) + 1;
}
else
byteList[byt] = 1;
}
var listByteData = byteList.OrderByDescending(r => r.Value).ToList();
foreach (var item in listByteData)
{
Invoke(new MethodInvoker(
delegate { listView1.Items.Add(new ListViewItem(new string[] { item.Key, item.Value.ToString() })); }
));
}
}
private int GetCount(Dictionary<string, int> byteList, string key)
{
int countVal;
byteList.TryGetValue(key, out countVal);
return countVal;
}
}
}