2013-07-28 38 views
1

如何知道trueCrypt卷是否安裝在計算機上?檢測新虛擬驅動器的創建時間

注意我已經知道可以掛載哪些文件。換句話說,可以安裝的唯一卷是:C:\Vol1.tc,C:\Vol2.tcC:\Vol3.tc

如何知道卷何時被卸除?

我設法通過使用.net類FileSystemWatcher來做到這一點。每當我卸下一卷時,我都會注意到 FileSystemWatcher.Changed發生了火災。

如何知道卷的安裝時間?

這是我遇到麻煩的地方! 我是否經常查詢驅動器並查看驅動器是否存在。 這聽起來像個糟糕的主意,因爲如果有人插入USB,並且窗口將驅動器號分配給它,我將會出錯。 如何知道何時創建新的虛擬驅動器?

爲什麼我需要這個?

我需要創建一個應用程序,用戶可以看到 TrueCrypt加密卷從他的手機安裝。我 缺少做的唯一的事情就是找出卷時,安裝...

+1

Windows發送當卷添加或刪除WM_DEVICECHANGE廣播消息。你需要在你的代碼中處理這個Windows消息。 –

回答

1

正如尤金Mayevski「EldoS公司評論說,你應該使用WM_DEVICECHANGE事件。

與我給你on this question答案組合,這裏是一個示例代碼:

class Program : System.Windows.Forms.Form 
{ 
    static void Main(string[] args) 
    { 
     System.Windows.Forms.Application.Run(new Program()); 
    } 

    const int WM_DEVICECHANGE = 0x0219; 
    const int NB_MOUNTED_VOLUMES = 26; 
    protected override void WndProc(ref System.Windows.Forms.Message m) 
    { 
     switch (m.Msg) 
     { 
      case WM_DEVICECHANGE: 
       ShowChange(); 
       break; 
     } 
     base.WndProc(ref m); 
    } 

    private String[] m_oldMount; 
    public Program() 
    { 
     m_oldMount = new String[NB_MOUNTED_VOLUMES]; 
     for (int i = 0; i < NB_MOUNTED_VOLUMES; i++) 
      m_oldMount[i] = String.Empty; 
    } 

    private void ShowChange() 
    { 
     String[] currentMount = GetMountList(); 
     for (int i = 0; i < NB_MOUNTED_VOLUMES; i++) 
     { 
      if (currentMount[i] != m_oldMount[i]) 
      { 
       if (String.IsNullOrEmpty(currentMount[i])) 
       { 
        Console.WriteLine("{0} : Dismount on {1}: => {2}", 
         DateTime.Now, 
         (char)('A' + i), 
         m_oldMount[i]); 
       } 
       else 
       { 
        Console.WriteLine("{0} : Mount on {1}: => {2}", 
         DateTime.Now, 
         (char)('A' + i), 
         currentMount[i]); 
       } 
      } 
     } 
     m_oldMount = currentMount; 
    } 
    private String[] GetMountList() 
    { 
     uint size = (uint)Marshal.SizeOf(typeof(MOUNT_LIST_STRUCT)); 
     IntPtr buffer = Marshal.AllocHGlobal((int)size); 
     uint bytesReturned; 
     IntPtr _hdev = CreateFile("\\\\.\\TrueCrypt", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); 
     bool bResult = DeviceIoControl(_hdev, TC_IOCTL_GET_MOUNTED_VOLUMES, buffer, size, buffer, size, out bytesReturned, IntPtr.Zero); 
     MOUNT_LIST_STRUCT mount = new MOUNT_LIST_STRUCT(); 
     Marshal.PtrToStructure(buffer, mount); 
     Marshal.FreeHGlobal(buffer); 

     return mount.wszVolume.Select(m => m.ToString()).ToArray(); 
    } 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] 
    class MOUNT_LIST_STRUCT 
    { 
     public readonly UInt32 ulMountedDrives; /* Bitfield of all mounted drive letters */ 
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 26)] 
     public readonly MOUNT_LIST_STRUCT_VOLUME_NAME[] wszVolume; /* Volume names of mounted volumes */ 
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 26)] 
     public readonly UInt64[] diskLength; 
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 26)] 
     public readonly int[] ea; 
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 26)] 
     public readonly int[] volumeType; /* Volume type (e.g. PROP_VOL_TYPE_OUTER, PROP_VOL_TYPE_OUTER_VOL_WRITE_PREVENTED, etc.) */ 
    } 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] 
    struct MOUNT_LIST_STRUCT_VOLUME_NAME 
    { 
     [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I2, SizeConst = 260)] 
     public readonly char[] wszVolume; /* Volume names of mounted volumes */ 

     public override string ToString() 
     { 
      return (new String(wszVolume)).TrimEnd('\0'); 
     } 
    } 

    public static int CTL_CODE(int DeviceType, int Function, int Method, int Access) 
    { 
     return (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) 
      | (Method)); 
    } 
    private static readonly uint TC_IOCTL_GET_MOUNTED_VOLUMES = (uint)CTL_CODE(0x00000022, 0x800 + (6), 0, 0); 

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)] 
    static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, 
    IntPtr lpInBuffer, uint nInBufferSize, 
    IntPtr lpOutBuffer, uint nOutBufferSize, 
    out uint lpBytesReturned, IntPtr lpOverlapped); 

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    public static extern IntPtr CreateFile(
     [MarshalAs(UnmanagedType.LPTStr)] string filename, 
     [MarshalAs(UnmanagedType.U4)] FileAccess access, 
     [MarshalAs(UnmanagedType.U4)] FileShare share, 
     IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero 
     [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, 
     [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes, 
     IntPtr templateFile); 
}