0

I have developed a windows service that sends a mail automatically when the Windows User logs in. Now I want to add the User's name in the body of that mail. How can I identify the user, using windows service? Is it possible? Please tell me about some properties allowed in Windows service (C#) which can identify the user's name.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • How have you solved the first problem? (understand when a new user logs in?) Knowing the mechanism, will be easier to understand how to proceed.. – Lorenzo Dematté Apr 05 '13 at 13:05

2 Answers2

0

The current user logged on to the system can be extracted as follows:

string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Here is the MSDN reference.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
-2

you can easily get the current user name when you try the following statement:

System.Environment.UserName.ToString();

There are also some other members of the Environment class you can use, see: http://msdn.microsoft.com/en-us/library/system.environment.aspx

Hope this answers your question.

  • This statement returns the user of the current process. If the user of some other process is required, the following discussion could be helpful: http://stackoverflow.com/questions/777548/how-do-i-determine-the-owner-of-a-process-in-c – Axel Kemper Apr 05 '13 at 11:01
  • Oops, didn't notice that the OP was talking about a service. That will probably return System or Administrator when using Environment.UserName – Rien Parhan Apr 05 '13 at 11:12