-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
209 lines (195 loc) · 7.87 KB
/
Main.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
namespace RDD
{
public partial class Main : Form
{
private DriveDetector driveDetector = null;
private List<DeviceInfo> devices = null;
public Main()
{
InitializeComponent();
driveDetector = new DriveDetector();
devices = new List<DeviceInfo>();
driveDetector.DeviceArrived += new DriveDetectorEventHandler(OnDriveArrived);
driveDetector.DeviceRemoved += new DriveDetectorEventHandler(OnDriveRemoved);
driveDetector.QueryRemove += new DriveDetectorEventHandler(OnQueryRemove);
ScanDevices();
BindRemovable(devices);
}
private void ScanDevices()
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.Directory = drive.Name;
deviceInfo.MachineName = Environment.MachineName;
if (drive.DriveType != DriveType.NoRootDirectory && drive.DriveType != DriveType.CDRom)
{
//deviceInfo.DeviceName = drive.VolumeLabel;
}
deviceInfo.Username = Environment.UserName;
deviceInfo.PluggedInTime = (drive.IsReady && drive.DriveType == DriveType.Removable) ? DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() : "";
deviceInfo.RemovedTime = "";
deviceInfo.PluggedInOrRemoved = (drive.IsReady || drive.DriveType == DriveType.Fixed || drive.DriveType == DriveType.CDRom) ? "Plugged In" : "Removed";
deviceInfo.DeviceType = drive.DriveType.ToString();
devices.Add(deviceInfo);
}
}
private void BindRemovable(List<DeviceInfo> devices)
{
string noDeviceAvailable = "No removable devices available";
int numberOfDevice = devices.Count;
if (numberOfDevice > 0)
{
lblFound.Text = string.Format("{0} devices had been detected.", numberOfDevice);
dgvDevices.DataSource = null;
dgvDevices.Update();
dgvDevices.Refresh();
dgvDevices.DataSource = devices;
}
else
{
lblFound.Text = noDeviceAvailable;
}
}
/// <summary>
/// Export to CSV file
/// Only CSV comma delimited files are supported
/// </summary>
private void Export2Csv(List<DeviceInfo> devices)
{
string noDeviceAvailable = "No removable devices available";
string filePath = ConfigurationManager.AppSettings["FilePath"];
string fileName = string.Empty;
StringBuilder sb = new StringBuilder();
if (devices.Count > 0)
{
//add CSV file header
sb.Append("USER NAME");
sb.Append(",");
sb.Append("MACHINE NAME");
sb.Append(",");
//sb.Append("DEVICE NAME");
//sb.Append(",");
sb.Append("DIRECTORY");
sb.Append(",");
sb.Append("PLUGGED IN TIME");
sb.Append(",");
sb.Append("REMOVED TIME");
sb.Append(",");
sb.Append("PLUGGED IN OR REMOVED");
sb.Append(",");
sb.Append("DEVICE TYPE");
sb.AppendLine();
foreach (DeviceInfo item in devices)
{
sb.Append(item.Username);
sb.Append(",");
sb.Append(item.MachineName);
sb.Append(",");
//sb.Append(item.DeviceName);
//sb.Append(",");
sb.Append(item.Directory);
sb.Append(",");
sb.Append(item.PluggedInTime);
sb.Append(",");
sb.Append(item.RemovedTime);
sb.Append(",");
sb.Append(item.PluggedInOrRemoved);
sb.Append(",");
sb.Append(item.DeviceType);
sb.AppendLine();
}
}
else
{
sb.AppendLine(noDeviceAvailable);
}
string csvData = sb.ToString();
if (!string.IsNullOrEmpty(csvData))
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
fileName = string.Concat(ConfigurationManager.AppSettings["FileName"], "_", DateTime.Now.ToString("yyyyMMddHHmmss"), ".csv");
string strFileLocation = Path.Combine(filePath, fileName);
File.WriteAllText(strFileLocation, csvData);
MessageBox.Show("Export to CSV file had been done, The CSV file was stored following " + strFileLocation + "!");
}
}
/// <summary>
/// Called by DriveDetector when removable device in inserted
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
{
// e.Drive is the drive letter for the device which just arrived, e.g. "E:\\"
string directory = e.Drive;
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.Directory = directory;
deviceInfo.MachineName = Environment.MachineName;
//deviceInfo.DeviceName = "";
deviceInfo.Username = Environment.UserName;
deviceInfo.PluggedInTime = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
deviceInfo.RemovedTime = "";
deviceInfo.PluggedInOrRemoved = "Plugged In";
deviceInfo.DeviceType = "Removable";
devices.Add(deviceInfo);
BindRemovable(devices);
}
/// <summary>
/// Called by DriveDetector after removable device has been unpluged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDriveRemoved(object sender, DriveDetectorEventArgs e)
{
string directory = e.Drive;
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.Directory = directory;
deviceInfo.MachineName = Environment.MachineName;
//deviceInfo.DeviceName = "";
deviceInfo.Username = Environment.UserName;
deviceInfo.PluggedInTime = "";
deviceInfo.RemovedTime = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
deviceInfo.PluggedInOrRemoved = "Removed";
deviceInfo.DeviceType = "Removable";
devices.Add(deviceInfo);
BindRemovable(devices);
}
/// <summary>
/// Called by DriveDetector when removable drive is about to be removed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnQueryRemove(object sender, DriveDetectorEventArgs e)
{
}
/// <summary>
/// Close
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Export to csv file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExport_Click(object sender, EventArgs e)
{
Export2Csv(devices);
}
}
}