我尝试将我们的.Net 4桌面应用程序迁移到UWP应用程序,这是一项艰巨的任务,没有像MSDN这样的文档。知道我有连接到现有Webservice的所有方法,但是使用的方法什么都不返回。
var envVars = Environment.GetEnvironmentVariables();
var host = Environment.GetEnvironmentVariable("COMPUTERNAME");
var user = Environment.GetEnvironmentVariable("USERNAME");
var userDnsDomain = Environment.GetEnvironmentVariable("USERDNSDOMAIN");
GetEnvironmentVariables的计数为0,而.Net中的旧方法不再存在:
var host = System.Net.Dns.GetHostName();
var user = Environment.UserName;
我需要计算机的名称,登录的用户名和域名。
是否有技巧或替代方案?
答案 0 :(得分:3)
我的解决方案:
var users = await User.FindAllAsync(UserType.LocalUser);
var user = (string) await users.FirstOrDefault().GetPropertyAsync(KnownUserProperties.AccountName);
var domain = "";
var host = "";
if (string.IsNullOrEmpty(user))
{
var domainWithUser = (string) await users.FirstOrDefault().GetPropertyAsync(KnownUserProperties.DomainName);
domain = domainWithUser.Split('\\')[0];
user = domainWithUser.Split('\\')[1];
}
var host = NetworkInformation.GetHostNames().First(x => x.Type == HostNameType.DomainName).DisplayName.Split('.')[0];
答案 1 :(得分:0)
此答案更像是警告。
User
API将适用于多种用户类型,因此您需要针对空值进行防御性编码。 我不知道为什么这个API如此不可靠。 这是一个示例MainPage和一个屏幕截图,显示了所有返回为空白的数据。
using System;
using System.Linq;
using Windows.System;
using Windows.UI.Xaml.Controls;
namespace App_1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
GetUserInfo();
}
public static async void GetUserInfo()
{
var users = await User.FindAllAsync(UserType.LocalUser);
var user = await users.FirstOrDefault().GetPropertyAsync(KnownUserProperties.AccountName);
var user_count = users.Count;
var AccountName =
await users[0].GetPropertyAsync(KnownUserProperties.AccountName);
var DisplayName =
await users[0].GetPropertyAsync(KnownUserProperties.DisplayName);
var DomainName =
await users[0].GetPropertyAsync(KnownUserProperties.DomainName);
var FirstName =
await users[0].GetPropertyAsync(KnownUserProperties.FirstName);
var GuestHost =
await users[0].GetPropertyAsync(KnownUserProperties.GuestHost);
var LastName =
await users[0].GetPropertyAsync(KnownUserProperties.LastName);
var PrincipalName =
await users[0].GetPropertyAsync(KnownUserProperties.PrincipalName);
var ProviderName =
await users[0].GetPropertyAsync(KnownUserProperties.ProviderName);
var SessionInitiationProtocolUri =
await users[0].GetPropertyAsync(KnownUserProperties.SessionInitiationProtocolUri);
var User_Type = users[0].Type;
}
}
}