-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathShimEnvironment.cs
88 lines (82 loc) · 3.12 KB
/
ShimEnvironment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) 2013-2015, Cureos AB.
* All rights reserved.
* http://www.cureos.com
*
* This file is part of Shim.
*
* Shim is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Shim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Shim. If not, see <http://www.gnu.org/licenses/>.
*/
namespace System
{
#if NETFX_CORE || WINDOWS_PHONE
using global::Windows.Networking;
using global::Windows.Networking.Connectivity;
#endif
/// <summary>
/// Shim complement for the <see cref="Environment"/> class, providing members that are
/// not included in the PCL member subset of the <see cref="Environment"/> class.
/// </summary>
public static class ShimEnvironment
{
/// <include file='_Doc/mscorlib.xml' path='doc/members/member[@name="P:System.Environment.MachineName"]/*' />
public static string MachineName
{
get
{
#if DOTNET
return Environment.MachineName;
#elif NETFX_CORE || WINDOWS_PHONE
// Want to store the hostname to send for push notifications to make
// the management UI better. Take the substring up to the first period
// of the first DomainName entry.
// Thanks to Jeff Wilcox and Matthijs Hoekstra
// Adapted from Q42.WinRT library at https://github.com/Q42/Q42.WinRT
var list = NetworkInformation.GetHostNames();
string name = null;
if (list.Count > 0)
{
foreach (var entry in list)
{
if (entry.Type == HostNameType.DomainName)
{
var s = entry.CanonicalName;
if (!String.IsNullOrEmpty(s))
{
// Domain-joined. Requires at least a one-character name.
var j = s.IndexOf('.');
if (j > 0)
{
name = s.Substring(0, j);
break;
}
// Typical home machine.
name = s;
}
}
}
}
if (String.IsNullOrEmpty(name))
{
// TODO: Localize?
name = "Unknown Windows 8";
}
return name;
#else
throw new PlatformNotSupportedException("PCL");
#endif
}
}
}
}