-3

I have a WPF application that needs to get the Windows username of the user. I am using the following line of code:

MessageBox.Show("Your username is:\n" + WindowsIdentity.GetCurrent().Name);

This works fine on my computer and computers of our other developers, but for others, it crashes. I'm assuming this is a permissions issue. Here is the error:

Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: notesformultipleproperties.exe
Problem Signature 02: 1.0.0.0
Problem Signature 03: 51cb04a1
Problem Signature 04: PresentationFramework
Problem Signature 05: 4.0.0.0
Problem Signature 06: 504dc7da
Problem Signature 07: 7b4d
Problem Signature 08: 0
Problem Signature 09: System.Windows.Markup.XamlParse
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt

Can I allow this line of code to run no matter what permissions the user has?

Ben Walker
  • 2,037
  • 5
  • 34
  • 56
  • Do you just need the user name alone, or will you also need domain information? `Environment.UserName` will _not_ provide the domain, while your current method will. – DonBoitnott Jun 26 '13 at 16:44

3 Answers3

3

How about Environment.UserName? It will return "the user name the current process is running under".

antonijn
  • 5,702
  • 2
  • 26
  • 33
2

You can use Environment.UserName instead of getting the WindowsIdentity. This requires fewer permissions, and will likely work without the elevated permissions you're currently requiring.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

This question has an accepted answer: How do I get the logged in user's window's credentials in a WPF application

Environment.UserName

Community
  • 1
  • 1
Sven Grosen
  • 5,616
  • 3
  • 30
  • 52
  • 3
    Then why post another one? – Clemens Jun 26 '13 at 16:45
  • Because I didn't have enough reputation at the time to mark as a duplicate or comment on the original post :) – Sven Grosen Jun 26 '13 at 16:48
  • nevertheless, this is not an answer. flagged. – tckmn Jun 26 '13 at 17:00
  • How is it not an answer? It provides the same answer as the other two answers (including the accepted one)? I just acknowledge the existence of another question with the same answer. – Sven Grosen Jun 26 '13 at 17:02
  • They're right. You should have left it in the comments of the original question. – Milne Jun 26 '13 at 17:05
  • As I mentioned originally, I didn't have enough reputation to do so when I posted this answer. If I did have the reputation at the time, I certainly would have done that. Ironically, this answer has bumped me over 50 reputation so now I can. – Sven Grosen Jun 26 '13 at 17:09