Académique Documents
Professionnel Documents
Culture Documents
User Interface:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace PenDriveManager { public partial class Form3 : Form { public Form3() { InitializeComponent(); }
private void label6_Click_1(object sender, EventArgs e) { if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit(); } }
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit();
62
APPENDICES
} } private void loadT_Click(object sender, EventArgs e) {
} private void exitToolStripMenuItem1_Click(object sender, EventArgs e) { if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit(); } } private void clearToolStripMenuItem_Click(object sender, EventArgs e) { PenDriveManager.Form3 frm = new PenDriveManager.Form3(); frm.Refresh();
} private void button1_Click(object sender, EventArgs e) { DriveInfo1.Form1 frm = new DriveInfo1.Form1(); frm.Show(); }
63
APPENDICES
frm.Show(); } private void button3_Click(object sender, EventArgs e) { Password.frmMain frm = new Password.frmMain(); frm.Show(); } private void button4_Click(object sender, EventArgs e) { PenDriveDataFormat.frmFormat frm = new PenDriveDataFormat.frmFormat(); frm.Show(); } private void button5_Click(object sender, EventArgs e) { if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit(); } }
64
APPENDICES
openFD.InitialDirectory = "C:"; openFD.ShowDialog(); } private void button13_Click(object sender, EventArgs e) { PenDriveManager.Form3 frm = new PenDriveManager.Form3();
frm.Refresh();
PenDrive.frmFormat frm1 = new PenDrive.frmFormat(); frm1.Refresh(); PenDriveDataFormat.frmFormat frm2 = new PenDriveDataFormat.frmFormat(); frm2.Refresh();
} private void button14_Click(object sender, EventArgs e) { PenDrive.frmFormat frm = new PenDrive.frmFormat(); frm.Show(); }
65
APPENDICES
} }
Detection:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace DriveInfo1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } float Piemark = 0; bool IsInfoAvailiable = false; private void Form1_Load(object sender, EventArgs e) { DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in DriveInfo.GetDrives()) { if (drive.DriveType == DriveType.Removable)
66
APPENDICES
{ cmbCombo.Items.Add(drive.Name);
} cmbCombo.SelectedIndex = -1; } } private void GroupBox3_Paint(object sender, PaintEventArgs e) { Rectangle rect = new Rectangle(130, 50, 120, 50); if (IsInfoAvailiable) { e.Graphics.FillPie(Brushes.Fuchsia, rect, 0, Piemark); e.Graphics.FillPie(Brushes.Blue, rect, Piemark, 360 - Piemark); Application.DoEvents(); } } private void cmbCombo_SelectedIndexChanged(object sender, EventArgs e) { System.IO.DriveInfo Drive_Info = null; if (cmbCombo.SelectedIndex != -1) { Drive_Info = new DriveInfo(cmbCombo.Text); lblName.Text = Drive_Info.Name; if (Drive_Info.IsReady) { if (Drive_Info.VolumeLabel.Length > 0) { lblVolumeLabel.Text = Drive_Info.VolumeLabel; }
67
APPENDICES
else { lblVolumeLabel.Text = "None"; } lblFileSystem.Text = Drive_Info.DriveFormat; lblType.Text = Drive_Info.DriveType.ToString(); lblRootDir.Text = Drive_Info.RootDirectory.FullName; lblCapacity.Text = Drive_Info.TotalSize + " (Bytes)"; lblAvalspace.Text = Drive_Info.TotalFreeSpace + " (Bytes)"; long usedSpace = 0; usedSpace = Drive_Info.TotalSize - Drive_Info.TotalFreeSpace; lblUsedSpace.Text = usedSpace + " (Bytes)"; //ProgressBar1.Value = 0 Piemark = 360f * Drive_Info.TotalFreeSpace / Drive_Info.TotalSize; long ProgressCurrentValue = usedSpace * 100 / Drive_Info.TotalSize; IsInfoAvailiable = true; //ProgressBar1.Value = ProgressCurrentValue //ProgressBar1.Show() } else { IsInfoAvailiable = false; lblFileSystem.Text = ""; lblType.Text = ""; lblRootDir.Text = ""; lblCapacity.Text = ""; lblAvalspace.Text = ""; lblUsedSpace.Text = ""; } GroupBox3.Invalidate(); if (IsInfoAvailiable == false) { MessageBox.Show("Drive is not ready"); } } }
68
APPENDICES
Information:
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.Management; using System.IO;
namespace PenDriveManager { public partial class frmInformation : Form { String _in; String _driveLetter; String _se; String _capacity; String _t; string _vendor; string _product; string _serialNumber; public frmInformation() { InitializeComponent();
69
APPENDICES
} private void Form1_Load(object sender, EventArgs e) { getSerialNumberFromDriveLetter();
matchDriveLetterWithSerial(); }
TextBox.Text = this._serialNumber; textBox1.Text = this._vendor; textBox2.Text = this._product; textBox3.Text = " (" + this._driveLetter + ")";
textBox4.Text = this._capacity;
70
APPENDICES
private void matchDriveLetterWithSerial() { string[] diskArray; string driveNumber; string driveLetter; ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition "); foreach (ManagementObject dm in searcher1.Get()) { diskArray = null;
driveLetter = getValueInQuotes(dm["Dependent"].ToString()); diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(','); driveNumber = diskArray[0].Remove(0, 6).Trim(); if (driveLetter == this._driveLetter || this._driveLetter == null) { /* This is where we get the drive serial */ ManagementObjectSearcher di = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); foreach (ManagementObject disk in di.Get()) { if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB")
71
APPENDICES
{ _driveLetter = driveLetter; this._product = parseProductNameFromDeviceID(disk["PNPDeviceID"].ToString());
this._vendor = parseVendorNameFromDeviceID(disk["PNPDeviceID"].ToString()); this._serialNumber = parseSerialFromDeviceID(disk["PNPDeviceID"].ToString()); _capacity = disk["Size"].ToString() + " bytes (" + Math.Round(((((double)Convert.ToDouble(disk["Size"]) / 1024) / 1024) / 1024), 2) + " GB)"; _in = disk["TotalCylinders"].ToString(); _se = disk["TotalSectors"].ToString(); _t = disk["TotalTracks"].ToString(); } } } } } private string getValueInQuotes(string inValue) { string parsedValue = ""; int posFoundStart = 0; int posFoundEnd = 0; posFoundStart = inValue.IndexOf("\""); posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1); parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd posFoundStart) - 1); return parsedValue;
72
APPENDICES
}
private string parseVendorNameFromDeviceID(string deviceId) { string[] splitDeviceId = deviceId.Split('\\'); string[] serialArray; string serial; int arrayLen = splitDeviceId.Length - 1; serialArray = splitDeviceId[arrayLen - 1].Split('&'); serial = serialArray[1].Remove(0, 4); return serial; }
private string parseSerialFromDeviceID(string deviceId) { string[] splitDeviceId = deviceId.Split('\\'); string[] serialArray; string serial; int arrayLen = splitDeviceId.Length - 1; serialArray = splitDeviceId[arrayLen].Split('&'); serial = serialArray[0]; return serial; } private string parseProductNameFromDeviceID(string deviceId) { string[] splitDeviceId = deviceId.Split('\\'); string[] serialArray; string serial; int arrayLen = splitDeviceId.Length - 1; serialArray = splitDeviceId[arrayLen - 1].Split('&'); serial = serialArray[2].Remove(0, 5); return serial;
73
APPENDICES
}
} }
74
APPENDICES
public static bool ConfirmPwd(string pwd) { string storedPwd, encryptedPwd; try { StreamReader fsPwdFile = new StreamReader( new FileStream( PenDriveManager.Program.strPwdFilePath, FileMode.Open, FileAccess.Read)); storedPwd = fsPwdFile.ReadToEnd(); fsPwdFile.Close(); } catch { return false; } encryptedPwd = MD5Hash(pwd); if (storedPwd != encryptedPwd) return false; else return true; } } } using System; using System.IO; using System.Windows.Forms; using Microsoft.Win32; namespace Password { public partial class frmMain : Form { public frmMain() {
75
APPENDICES
InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { CheckPasswordStatus(); USB_getStatus(); } private void CheckPasswordStatus() { PenDriveManager.Program.strPwdFilePath += "\\usbpassword.pwd"; if (File.Exists(PenDriveManager.Program.strPwdFilePath)) { try { StreamReader fsPwdFile = new StreamReader( new FileStream(PenDriveManager.Program.strPwdFilePath, FileMode.Open, FileAccess.Read)); string pwd = fsPwdFile.ReadToEnd(); if (String.IsNullOrEmpty(pwd) == false) PenDriveManager.Program.isPwdEnabled = true; fsPwdFile.Close(); } catch { } } else PenDriveManager.Program.isPwdEnabled = false; }
76
APPENDICES
key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies"); if (System.Convert.ToInt16(key.GetValue("WriteProtect", null)) == 1) USB_radio_ReadOnly.Checked = true; else USB_radio_FullAccess.Checked = true; } catch (NullReferenceException ) { key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\Control", true); key.CreateSubKey("StorageDevicePolicies"); key.Close(); } catch (Exception) { } }
void USB_enableWriteProtect() { RegistryKey key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies", true); if (key == null) { Registry.LocalMachine.CreateSubKey ("SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies", RegistryKeyPermissionCheck.ReadWriteSubTree); key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies", true); key.SetValue("WriteProtect", 1, RegistryValueKind.DWord); } else if (key.GetValue("WriteProtect") != (object)(1)) {
77
APPENDICES
key.SetValue("WriteProtect", 1, RegistryValueKind.DWord); } } void USB_disableWriteProtect() { RegistryKey key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\Control\\StorageDevicePolicies", true); if (key != null) { key.SetValue("WriteProtect", 0, RegistryValueKind.DWord); } key.Close(); } private void btnApply_Click(object sender, EventArgs e) { DialogResult resultApplyChanges = MessageBox.Show ("Apply changes?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (resultApplyChanges == DialogResult.Yes) { if (PenDriveManager.Program.isPwdEnabled) { frmPasswordCheck frmPwdCheck = new frmPasswordCheck(); frmPwdCheck.ShowDialog(); if (frmPwdCheck.DialogResult != DialogResult.OK) return; } if (USB_radio_ReadOnly.Checked == true) { USB_enableWriteProtect(); } if (USB_radio_FullAccess.Checked == true) {
78
APPENDICES
USB_disableWriteProtect(); }
MessageBox.Show ("In order to enable new setting please" + " reconnect your USB storage devices" + " or restart your computer", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void btnRestore_Click(object sender, EventArgs e) { USB_getStatus(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void btnPassword_Click(object sender, EventArgs e) { frmPassword PasswordForm = new frmPassword(); PasswordForm.ShowDialog(); }
} } using System; using System.IO; using System.Windows.Forms; namespace Password { public partial class frmPasswordCheck : Form
79
APPENDICES
{ public frmPasswordCheck() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { if (CommonMethods.ConfirmPwd( txtPwd.Text )) { this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show ("Wrong password, please re-enter", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtPwd.Focus(); return; } } private void btnClear_Click(object sender, EventArgs e) { txtPwd.Clear(); txtPwd.Focus(); } private void btnClose_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } } }
80
APPENDICES
using System; using System.IO; using System.Windows.Forms; using System.Security.Cryptography; namespace Password { public partial class frmPassword : Form { public frmPassword() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { if( txtNewPwd.Text != txtNewPwdAgain.Text ) { MessageBox.Show ("Passwords do not match, please retry","Error", MessageBoxButtons.OK,MessageBoxIcon.Error); txtNewPwdAgain.Clear(); txtNewPwd.Clear(); txtNewPwd.Focus(); return; } if(PenDriveManager.Program.isPwdEnabled ) if (CommonMethods.ConfirmPwd( txtOldPwd.Text) == false) { MessageBox.Show ("Wrong password, please retry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtOldPwd.Focus(); return;
81
APPENDICES
} if (String.IsNullOrEmpty(txtNewPwd.Text)) { DisablePwd(); MessageBox.Show ("Password disabled", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); return; } string newPwdEncrypted = CommonMethods.MD5Hash( txtNewPwd.Text ); StorePassword(newPwdEncrypted); PenDriveManager.Program.isPwdEnabled = true; MessageBox.Show ("Password changed successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } private void DisablePwd() { PenDriveManager.Program.isPwdEnabled = false; try { File.Delete(PenDriveManager.Program.strPwdFilePath); } catch { } } private void StorePassword(string newPwdEncrypted) { try {
82
APPENDICES
File.Delete(PenDriveManager.Program.strPwdFilePath); StreamWriter fsPwdFile = new StreamWriter( new FileStream (PenDriveManager.Program.strPwdFilePath, FileMode.Create, FileAccess.Write)); fsPwdFile.Write(newPwdEncrypted); fsPwdFile.Close(); } catch { } } private void frmPassword_Load(object sender, EventArgs e) { if (PenDriveManager.Program.isPwdEnabled == false) txtOldPwd.Enabled = false; } private void btnClear_Click(object sender, EventArgs e) { txtNewPwd.Clear(); txtOldPwd.Clear(); txtOldPwd.Focus(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); }
} }
Format:
DEPARTMENT OF INFORMATION TECHNOLOGY
83
APPENDICES
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Linq;
listBox2.Visible = false;
84
APPENDICES
foreach (string d in dir) { listBox1.Items.Add(d); } string[] fi = Directory.GetFiles(driveLetter); string filename = ""; foreach (string f in fi) {
filename = Path.GetFileName(f);
85
APPENDICES
private void button1_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure?", "Format ", MessageBoxButtons.OKCancel) == DialogResult.OK) { try { listBox2.Visible = false; label2.Visible = false; PenDriveSerialNumber.SerialNumber obj = new PenDriveSerialNumber.SerialNumber(); String strCode = obj.getSerialNumberFromDriveLetter(); TextBox.Text = strCode; string driveLetter = obj.driveLetter;
86
APPENDICES
{ Directory.Delete(d, true); progressBar1.Minimum = 0; progressBar1.Maximum = dir.Length; progressBar1.Value = listBox2.Items.Count; progressBar1.Refresh(); int percent = (int)(((double)(progressBar1.Value progressBar1.Minimum) / (double)(progressBar1.Maximum - progressBar1.Minimum)) * 100); using (Graphics gr = progressBar1.CreateGraphics()) { gr.DrawString(percent.ToString() + "%", new Font("Arial", 16), Brushes.Black, new PointF(progressBar1.Width / 2 (gr.MeasureString(percent.ToString() + "%", new Font("Arial", 16)).Width / 2.0F), progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%", new Font("Arial", 16)).Height / 2.0F))); } listBox2.Items.Add(d); } listBox1.Items.Clear();
label2.Visible = true; label2.Text = " All The File(s) Are Formatted: "; progressBar1.Visible = false;
87
APPENDICES
MessageBox.Show("Done!"); }
catch (Exception) { Console.WriteLine(MessageBox.Show("The process failed: ")); } } } private void btnOK_Click(object sender, EventArgs e) { this.Close(); }
} }
using System; using System.Collections.Generic; using System.Text; using System.Management; namespace PenDriveSerialNumber {
88
APPENDICES
public class SerialNumber { private String _driveLetter; public String driveLetter { get { return _driveLetter; } set { _driveLetter = value; } }
89
APPENDICES
}
private void matchDriveLetterWithSerial() { string[] diskArray; string driveNumber; string driveLetter; ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition"); foreach (ManagementObject dm in searcher1.Get()) { diskArray = null; driveLetter = getValueInQuotes(dm["Dependent"].ToString()); diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(','); driveNumber = diskArray[0].Remove(0, 6).Trim(); if (driveLetter == this._driveLetter || this._driveLetter == null) { /* This is where we get the drive serial */ ManagementObjectSearcher disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); foreach (ManagementObject disk in disks.Get()) { if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB") { _driveLetter = driveLetter; this._product = parseProductNameFromDeviceID(disk["PNPDeviceID"].ToString());
this._vendor = parseVendorNameFromDeviceID(disk["PNPDeviceID"].ToString());
90
APPENDICES
} } } } private string parseVendorNameFromDeviceID(string deviceId) { string[] splitDeviceId = deviceId.Split('\\'); string[] serialArray; string serial; int arrayLen = splitDeviceId.Length - 1; serialArray = splitDeviceId[arrayLen - 1].Split('&'); serial = serialArray[1].Remove(0, 4); return serial; }
private string parseProductNameFromDeviceID(string deviceId) { string[] splitDeviceId = deviceId.Split('\\'); string[] serialArray; string serial; int arrayLen = splitDeviceId.Length - 1; serialArray = splitDeviceId[arrayLen - 1].Split('&'); serial = serialArray[2].Remove(0, 5); return serial; } private string getValueInQuotes(string inValue) { string parsedValue = ""; int posFoundStart = 0; int posFoundEnd = 0;
91
APPENDICES
posFoundStart = inValue.IndexOf("\""); posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1); parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd posFoundStart) - 1); return parsedValue; } } }
92