{"id":97745,"date":"2018-01-09T07:00:00","date_gmt":"2018-01-09T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97745"},"modified":"2019-03-13T01:02:19","modified_gmt":"2019-03-13T08:02:19","slug":"20180109-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180109-00\/?p=97745","title":{"rendered":"How do I get the computer&#8217;s serial number? Consuming Windows Runtime classes in desktop apps, part 4: C#"},"content":{"rendered":"<p>Continuing our series on getting the computer&#8217;s serial number in desktop apps in various languages, we look at C#. <\/p>\n<p>From Visual Studio, create a new C# Console Application that goes like this: <\/p>\n<pre>\nclass Program\n{\n  static public void Main()\n  {\n    var serialNumber = Windows.System.Profile.SystemManufacturers.\n                       SmbiosInformation.SerialNumber;\n    System.Console.WriteLine($\"Serial number = {serialNumber}\");\n  }\n}\n<\/pre>\n<p>Before building, you&#8217;ll have to prepare the project, and the preparation is particularly ugly. <\/p>\n<ul>\n<li>\n<p>Close the solution in Visual Studio and open the <code>*.csproj<\/code> file in a text editor. <\/p>\n<\/li>\n<li>\n<p>Add <\/p>\n<pre>\n    &lt;TargetPlatformVersion&gt;8.0&lt;\/TargetPlatformVersion&gt;\n<\/pre>\n<p>to the main <code>PropertyGroup<\/code>. This requirement is <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/hh708954(v=vs.110).aspx\">obscurely documented on MSDN<\/a>. <a HREF=\"https:\/\/www.hanselman.com\/blog\/HowToCallWinRTAPIsInWindows8FromCDesktopApplicationsWinRTDiagram.aspx\">Scott Hanselman tipped me off<\/a>. <\/p>\n<\/li>\n<li>\n<p>Reopen the project, right click the References node and select <i>Add Reference<\/i>. <\/p>\n<\/li>\n<li>\n<p>The magic XML you added to the <code>*.csproj<\/code> enables a new node in the dialog box called <i>Windows<\/i>. Expand it, click on <i>Core<\/i>, and then check <code>Windows.<\/code><code>System<\/code> because we are using <code>Windows.<\/code><code>System.<\/code><code>BlahBlah<\/code>. In general, check each second-level namespace your program uses. <\/p>\n<\/li>\n<\/ul>\n<p>Adding a reference from <i>Core<\/i> will access the information from your development machine, so it assumes that your development machine is running the same or greater version of Windows than your target. If you are doing cross-targeting, then instead of referencing the <code>Windows.<\/code><code>Blah<\/code> namespaces under <i>Core<\/i>, go to the <i>Browse<\/i> option and browse to <code>C:\\<\/code><code>Program Files (x86)\\<\/code><code>Windows Kits\\<\/code><code>10\\<\/code><code>References\\<\/code><code>CONTRACT\\<\/code><code>VERSION\\<\/code><code>CONTRACT.winmd<\/code>. <\/p>\n<p>In our case, <code>Windows.<\/code><code>System.<\/code><code>Profile.<\/code><code>SystemManufacturers.<\/code><code>SmbiosInformation<\/code> is in the <code>Windows.<\/code><code>System.<\/code><code>Profile.<\/code><code>System&shy;Manufacturers.<\/code><code>System&shy;Manufacturers&shy;Contract<\/code> contract. I got this information from <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/Windows.System.Profile.SystemManufacturers.SmbiosInformation\">the documentation for the <code>Smbios&shy;Information<\/code> class<\/a>: Look under <b>API contract<\/b>. <\/p>\n<p>That documentation also says that the <code>Smbios&shy;Information<\/code> class<\/a> was introduced in v1, so the minimum version I need is 1.0.0.0. The full path is therefore <\/p>\n<pre>\nC:\\Program Files (x86)\\\n  Windows Kits\\\n    10\\\n      References\\\n        Windows.System.Profile.SystemManufacturers.SystemManufacturersContract\\\n          1.0.0.0\\\n            Windows.System.Profile.SystemManufacturers.SystemManufacturersContract.winmd\n<\/pre>\n<p>Repeat for each contract your program requires. Most classes are in the <code>Windows.<\/code><code>Foundation.<\/code><code>Universal&shy;Api&shy;Contract<\/code> contract. <\/p>\n<p>The last bit is another <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/apps\/jj856306.aspx\">obscure piece of information on MSDN<\/a>: Adding a reference to <code>System.Runtime<\/code>. If you use a Windows Runtime class that projects as an <code>IDictionary<\/code> or some other fancy type, then you will get the error message &#8220;The type &#8216;IDictionary`2&#8217; is defined in an assembly that is not referenced. You must add a reference to assembly &#8216;System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&#8217;.&#8221; But when you go to the <i>Add Reference<\/i> dialog, you won&#8217;t see it! It&#8217;s telling me to add a reference to an assembly that doesn&#8217;t exist! <\/p>\n<p>That&#8217;s because it&#8217;s hidden away somewhere that Visual Studio doesn&#8217;t show you. Go to the <i>Browse<\/i> tab, click the Browse button, and then go to <code>%ProgramFiles(x86)%\\<\/code><code>Reference Assemblies\\<\/code><code>Microsoft\\<\/code><code>Framework.<\/code><code>NETFramework\\<\/code><code>v4.5\\<\/code><code>Facades\\<\/code><code>System.Runtime.dll<\/code>, substituting the version of the .NET Framework that applies to your project. <\/p>\n<p>Okay, now that you got all the references set up, you can build and run the program. <\/p>\n<p>It takes some work to set up, but personally I find C# to be the most convenient way of consuming Windows Runtime classes. <\/p>\n<p>Next up is PowerShell. Just warning you ahead of time: You&#8217;re going to be underwhelmed. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over into managed code.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-97745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Over into managed code.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=97745"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97745\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=97745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}