如何在WPF中获取主机名 [英] how to get host name in wpf
问题描述
我可以知道是否有人知道在WPF中哪个接口可以获取主机用户名吗?例如:我创建一个项目,并使用静态字符串初始化该项目,就像:C:\ user \ lcuser \ xxx.但是,如果我将该项目移动到另一台主机名为"mslcuser"的PC上,则我确信该项目将在初始化中失败,因为该字符串必须为:C:\ user \ msuser \ xxx.那么如何获得主机名.任何建议将不胜感激.
May I know if anyone knows which interface could get host user name in WPF? For example: I creat a project and the item initialized with static string just like : C:\user\lcuser\xxx. But if I move this project in another PC with host-name called "mslcuser", I am sure this item will fail in initialization because the string have to be : C:\user\msuser\xxx. So how to get the host-name. Any suggestions will be greatly appreciated.
推荐答案
那不是主机名",而是用户名"-很大的不同!主机名标识计算机.另外,"C:\ User"库位于其他位置,并且Windows XP也已本地化(例如C:\ Dokumente和Einstellungen \ Name \ ....).
您需要的是Environment.SpecialFolder
枚举,您可以通过以下方式获取相应的路径:Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
That''s not the "host name", but the "user name" - a big difference! The host name identifies the computer. Also, "C:\User" coukld be located elsewhere, and with Windows XP was also localised (e.g. C:\Dokumente und Einstellungen\Name\....).
What you need is theEnvironment.SpecialFolder
enumeration, and you get the corresponding path by e.g.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
我认为要进行类似您应该使用APP_DATA文件夹的操作.这将适当照顾用户/主机名.
可以通过以下方式获取APP_DATA文件夹:
I think for doing operations like you should use APP_DATA folder. This will appropriately take care of user/host names.
APP_DATA folder can be obtained by :
Environment.GetFolderPath Method(...)
以,ApplicationData''作为第一个参数
with ,ApplicationData'' as first argument
这篇关于如何在WPF中获取主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!