Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 151, comments - 0, trackbacks - 0

My Links

News

Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories

Archives

Post Categories

.NET Framework

Blogroll

Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

WPF: Initializing My.User

If you use the My namespace from Visual Basic in WPF applications, especially the User object which allows accessing properties about the logged user, you might encounter a small problem. While in Windows Forms the following line returns the user name in the form of Domain\UserName:

Dim userName As String = My.User.Name

in WPF the same line of code returns an empty string. So, how can we solve this? there are basically two solutions. The first one is initializing My.User as follows:

My.User.InitializeWithWindowsUser()

'then invoke My.User.Name

Dim userName As String = My.User.Name

The second solution is utilizing the WindowsIdentity class as follows:

Dim userName As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name

If you like My, maybe you will prefer the first solution. In both cases the returned string for the user name is populated correctly :-)

Alessandro

Print | posted on giovedì 22 aprile 2010 14:47 | Filed Under [ Visual Basic Windows Presentation Foundation ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 4 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET