UsbTests.cs :  » Development » iTuner » iTunerTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » iTuner 
iTuner » iTunerTests » UsbTests.cs
//************************************************************************************************
// Copyright  2010 Steven M. Cohn. All Rights Reserved.
//
//************************************************************************************************

namespace iTunerTests{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.IO;
  using System.Linq;
  using System.Management;
  using System.Threading;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using iTuner;


  /// <summary>
  /// </summary>

  [TestClass]
  public class UsbTests : TestBase
  {
    private UsbManager manager;

    [TestMethod]
    public void CollectionTest ()
    {
      UsbDiskCollection disks = new UsbDiskCollection();
      Assert.IsFalse(disks.Contains("foo"));

      UsbDisk disk = new UsbDisk("foo");
      disks.Add(disk);

      Assert.IsTrue(disks.Contains(disk));
      Assert.IsTrue(disks.Contains("foo"));
      Assert.IsFalse(disks.Contains("missing"));

      disks.Remove("foo");
      Assert.AreEqual(0, disks.Count);
      Assert.IsFalse(disks.Contains("foo"));
    }


    [TestMethod]
    public void ManagerTest ()
    {
      manager = new UsbManager();
      UsbDiskCollection disks = manager.GetAvailableDisks();

      foreach (UsbDisk disk in disks)
      {
        Console.WriteLine(String.Format(
          "{0} {1} ({2})", disk.Name, disk.Volume, disk.Model));
      }

      // need to set GetDiskInformation to 'public' for these lines:
      //UsbDisk d = new UsbDisk("T:", null, null);
      //manager.GetDiskInformation(d);

      manager.StateChanged += new UsbStateChangedEventHandler(manager_StateChanged);

      ManualResetEvent reset = new ManualResetEvent(false);
      reset.WaitOne(120000, false);
    }

    private void manager_StateChanged (UsbStateChangedEventArgs e)
    {
      Console.WriteLine(e.State.ToString() + " - " + e.Disk.ToString());
      Debug.WriteLine(e.State.ToString() + " - " + e.Disk.ToString());
    }


    /// <summary>
    /// </summary>

    [Ignore]
    [TestMethod]
    public void DetectUSbDevices ()
    {
      DriveInfo[] drives = DriveInfo.GetDrives();
      Assert.IsNotNull(drives);

      foreach (DriveInfo drive in drives)
      {
        if (drive.IsReady)
        {
          Console.WriteLine(String.Format(
            "Drive {0} ({1})", drive.Name, drive.VolumeLabel));
        }
      }

      Console.WriteLine(new String('-', 80));

      // browse all USB WMI physical disks
      foreach (ManagementObject drive in
        new ManagementObjectSearcher(
          "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
      {
          // associate physical disks with partitions
        ManagementObject partition = new ManagementObjectSearcher(String.Format(
          "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
          drive["DeviceID"])).First();

        if (partition != null)
        {
              // associate partitions with logical disks (drive letter volumes)
          ManagementObject disk = new ManagementObjectSearcher(String.Format(
            "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
            partition["DeviceID"])).First();

          if (disk != null)
          {
            ManagementObject volume = new ManagementObjectSearcher(String.Format(
              "select VolumeName from Win32_LogicalDisk where Name='{0}'",
              disk["Name"])).First();

            Console.WriteLine(String.Format(
              "{0} {1} ({2})", disk["Name"], volume["VolumeName"], drive["Model"]));
          }
        }
      }
    }
  }


  public static class WmiExtensions
  {
    public static ManagementObject First (this ManagementObjectSearcher searcher)
    {
      ManagementObject result = null;
      foreach (ManagementObject item in searcher.Get())
      {
        result = item;
        break;
      }
      return result;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.