Advertisement
icorrelate

USB File Copier

May 24th, 2017
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.46 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace USBFilesCopier
  15. {
  16.     public partial class UsbFilesCopier : Form
  17.     {
  18.         /*
  19.         [DllImport("user32.dll")]
  20.         public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
  21.         [DllImport("user32.dll")]
  22.         public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  23.  
  24.         s*/
  25.  
  26.         /*
  27.         const int Exit_Hotkey_Id = 1;
  28.         const int AddToStartup_Hotkey_Id = 2;
  29.         const int RemoveToStartup_Hotkey_Id = 3;
  30.         */
  31.         public UsbFilesCopier()
  32.         {
  33.             InitializeComponent();
  34.  
  35.  
  36.  
  37.             // Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
  38.             // Compute the addition of each combination of the keys you want to be pressed
  39.             // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6...
  40.             // The author planned to use Hotkey in adding , removing the app to startup and exiting. Re use this codes if you want to enable the feature.
  41.  
  42.  
  43.             /*
  44.  
  45.             RegisterHotKey(this.Handle, AddToStartup_Hotkey_Id, 6, (int)Keys.D5);
  46.             RegisterHotKey(this.Handle, RemoveToStartup_Hotkey_Id, 6, (int)Keys.D6);
  47.             RegisterHotKey(this.Handle, Exit_Hotkey_Id, 6, (int)Keys.D7);
  48.  
  49.             */
  50.  
  51.  
  52.  
  53.         }
  54.  
  55.  
  56.         private const int WM_DEVICECHANGE = 0x219;
  57.         private const int DBT_DEVICEARRIVAL = 0x8000;
  58.         private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
  59.         private const int DBT_DEVTYP_VOLUME = 0x00000002;
  60.  
  61.         [StructLayout(LayoutKind.Sequential)]
  62.         public struct DevBroadcastVolume
  63.         {
  64.             public int Size;
  65.             public int DeviceType;
  66.             public int Reserved;
  67.             public int Mask;
  68.             public Int16 Flags;
  69.         }
  70.        
  71.  
  72.         protected override void WndProc(ref Message m)
  73.         {
  74.  
  75.  
  76.             /*
  77.             if (m.Msg == 0x0312 && m.WParam.ToInt32() == Exit_Hotkey_Id)
  78.             {
  79.                 Application.Exit();
  80.             }
  81.  
  82.             if (m.Msg == 0x0312 && m.WParam.ToInt32() == AddToStartup_Hotkey_Id)
  83.             {
  84.                 RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  85.                 rk.SetValue("USBFilesCopier", Application.ExecutablePath.ToString());
  86.                 MessageBox.Show("USBFilesCopier added to Startup");
  87.             }
  88.             if (m.Msg == 0x0312 && m.WParam.ToInt32() == RemoveToStartup_Hotkey_Id)
  89.             {
  90.                 RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  91.                 rk.DeleteValue("USBFilesCopier", false);
  92.                 MessageBox.Show("USBFiles Copier removed to Startup");
  93.             }
  94.  
  95.             */
  96.             base.WndProc(ref m);
  97.  
  98.  
  99.             switch (m.Msg)
  100.             {
  101.                 case WM_DEVICECHANGE:
  102.                     switch ((int)m.WParam)
  103.                     {
  104.                         case DBT_DEVICEARRIVAL:
  105.                             int devType = Marshal.ReadInt32(m.LParam, 4);
  106.                             if (devType == DBT_DEVTYP_VOLUME)
  107.                             {
  108.                                 DevBroadcastVolume vol;
  109.                                 vol = (DevBroadcastVolume)
  110.                                    Marshal.PtrToStructure(m.LParam,
  111.                                    typeof(DevBroadcastVolume));
  112.  
  113.                                 int mask = vol.Mask; String binaryMask = Convert.ToString(vol.Mask, 2);
  114.                                 int str = binaryMask.Length - binaryMask.IndexOf('1') - 1;
  115.                                 char letter = (char)('A' + str);
  116.  
  117.  
  118.                                 DriveInfo dinfo = getUsbInfo(letter.ToString());
  119.                                 string destination = "C:/Usb/" + dinfo.VolumeLabel + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
  120.                                 if (!Directory.Exists(destination))
  121.                                     Directory.CreateDirectory(destination);
  122.  
  123.                                 string path = destination + "/DriveInfo.txt";
  124.                                 if (!File.Exists(path))
  125.                                 {
  126.                                     using (StreamWriter sw = File.CreateText(path))
  127.                                     {
  128.  
  129.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Available Free Space: ", dinfo.AvailableFreeSpace));
  130.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Total Size: ", dinfo.TotalFreeSpace));
  131.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Total Size: ", dinfo.TotalSize));
  132.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Drive Format: ", dinfo.DriveFormat));
  133.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Drive Type: ", dinfo.DriveType));
  134.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Name: ", dinfo.Name));
  135.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Root Directory: ", dinfo.RootDirectory));
  136.                                         sw.WriteLine(string.Format("{0, -30}{1, -15}", "Volume Label: ", dinfo.VolumeLabel));
  137.                                     }
  138.                                 }
  139.  
  140.                                 DirectoryInfo source = new DirectoryInfo(dinfo.Name);
  141.                                 DirectoryInfo target = new DirectoryInfo(destination);
  142.                                 CopyAll(source, target);
  143.                             }
  144.  
  145.                             break;
  146.  
  147.                         case DBT_DEVICEREMOVECOMPLETE:
  148.                             break;
  149.  
  150.                     }
  151.                     break;
  152.             }
  153.  
  154.  
  155.  
  156.  
  157.         }
  158.         private DriveInfo getUsbInfo(string driveLetter)
  159.         {
  160.             DriveInfo info = new DriveInfo(driveLetter);
  161.             return info;
  162.         }
  163.  
  164.         public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
  165.         {
  166.             try
  167.             {
  168.                 Directory.CreateDirectory(target.FullName);
  169.                 foreach (FileInfo fi in source.GetFiles())
  170.                 {
  171.                     fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
  172.                 }
  173.                 foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  174.                 {
  175.                     DirectoryInfo nextTargetSubDir =
  176.                         target.CreateSubdirectory(diSourceSubDir.Name);
  177.                     CopyAll(diSourceSubDir, nextTargetSubDir);
  178.                 }
  179.             }
  180.             catch (Exception ex) { }
  181.  
  182.         }
  183.  
  184.         private void Form1_Load(object sender, EventArgs e)
  185.         {
  186.             this.ShowInTaskbar = false;
  187.             this.WindowState = FormWindowState.Minimized;
  188.             this.Visible = false;
  189.         }
  190.  
  191.         private void UsbFilesCopier_SizeChanged(object sender, EventArgs e)
  192.         {
  193.             if (this.WindowState == FormWindowState.Minimized)
  194.             {
  195.                 notifyIcon1.BalloonTipTitle = "USB File Copier";
  196.             }
  197.         }
  198.  
  199.         private void addToStartupToolStripMenuItem_Click(object sender, EventArgs e)
  200.         {
  201.             RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  202.             rk.SetValue("USBFilesCopier", Application.ExecutablePath.ToString());
  203.             notifyIcon1.BalloonTipText = "USBFiles Copier added to Startup";
  204.             notifyIcon1.ShowBalloonTip(1000);
  205.         }
  206.  
  207.         private void removeToStartupToolStripMenuItem_Click(object sender, EventArgs e)
  208.         {
  209.             RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  210.             rk.DeleteValue("USBFilesCopier", false);
  211.             notifyIcon1.BalloonTipText = "USBFiles Copier removed to Startup";
  212.             notifyIcon1.ShowBalloonTip(1000);
  213.         }
  214.  
  215.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  216.         {
  217.             Application.Exit();
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement