title: C# Creating a unique ID based on hardware ids
body: <p>I am creating a license that is specific to a machine. The license is based on the following items:</p>
  
  <ol>
  <li>MAC Address</li>
  <li>CPU Serial Number</li>
  <li>Computer Volume Serial Number of drive0 </li>
  </ol>
  
  <p>I am assuming that if 2 of the 3 match, then my license is valid. So, the can get a new network card, and the license is still valid, etc.</p>
  
  <p>Is this a good approach or am I going to have issues with this not matching or changing regularly?</p>
  
  <p>I'm trying to get a unique identifier for the computer so that I can validate the license.</p>
  
  <p>Please let me know how this looks or if you have a better solution!</p>
  
  <p>Thanks again!</p>
  
  <p>** HERE IS WHAT I CAME UP WITH **</p>
  
  <p>I ended up only using the VolumeSerial, CpuId, and VideoControllerDescription.</p>
  
  <pre><code>    public enum HardwareProfileComponents
      {
          ComputerModel,
          VolumeSerial,
          CpuId,
          MemoryCapacity,
          VideoControllerDescription
      }
  
      public static Dictionary&lt;string, string&gt; HardwareProfile()
      {
          var retval = new Dictionary&lt;string, string&gt;
                           {
                               {HardwareProfileComponents.ComputerModel.ToString(), GetComputerModel()},
                               {HardwareProfileComponents.VolumeSerial.ToString(), GetVolumeSerial()},
                               {HardwareProfileComponents.CpuId.ToString(), GetCpuId()},
                               {HardwareProfileComponents.MemoryCapacity.ToString(), GetMemoryAmount()},
                               {HardwareProfileComponents.VideoControllerDescription.ToString(), GetVideoControllerDescription()}
                           };
          return retval;
  
      }
  
      private static string GetVideoControllerDescription()
      {
          Console.WriteLine("GetVideoControllerDescription");
  
          var s1 = new ManagementObjectSearcher("select * from Win32_VideoController");
          foreach (ManagementObject oReturn in s1.Get())
          {
              var desc = oReturn["AdapterRam"];
              if ( desc == null) continue;
              return oReturn["Description"].ToString().Trim();
          }
          return string.Empty;
      }
  
  
      private static string GetComputerModel()
      {
          Console.WriteLine("GetComputerModel");
          var s1 = new ManagementObjectSearcher("select * from Win32_ComputerSystem"); 
          foreach (ManagementObject oReturn in s1.Get())
          {
              return oReturn["Model"].ToString().Trim();
          }            
          return string.Empty;
      }
  
      private static string GetMemoryAmount()
      {
          Console.WriteLine("GetMemoryAmount");
          var s1 = new ManagementObjectSearcher("select * from Win32_PhysicalMemory");
          foreach (ManagementObject oReturn in s1.Get())
          {
              return oReturn["Capacity"].ToString().Trim();
          }
          return string.Empty;
      }
  
      private static string GetVolumeSerial()
      {
          Console.WriteLine("GetVolumeSerial");
          var disk = new ManagementObject(@"win32_logicaldisk.deviceid=""c:""");
          disk.Get();
  
          string volumeSerial = disk["VolumeSerialNumber"].ToString();
          disk.Dispose();
  
          return volumeSerial;
      }
  
      private static string GetCpuId()
      {
          Console.WriteLine("GetCpuId");
          var managClass = new ManagementClass("win32_processor");
          var managCollec = managClass.GetInstances();
  
          foreach (ManagementObject managObj in managCollec)
          {
              //Get only the first CPU's ID
              return managObj.Properties["processorID"].Value.ToString();
          }
          return string.Empty;
      }
  </code></pre>