There may be many users on the local machine, and several of them can "log in", but only one of them will be active (the one who is working at the computer now). So the question is, can I find out from the code the current user is currently active or not? My programs work in the tray and periodically asks the question to which the user must answer, but you need to know with which account they are currently working.
2 answers
No, there is no such thing - “current active user”. Windows is a multi-user system, several logon sessions can be active at the same time.
Here is Raymond Chen's ancient article about this: Who says here only one? There can be more than one logon session .
Examples from there:
- Terminal Services means remote logins, while there may be no login on the physical machine (yes, there might not be a monitor).
- Media Center Extender allows another user to watch a movie in another room.
Show the question to all users, when inactive, remove it after a while.
- maybe I misunderstood the question. And how can you show all users? If the program is running from under a particular user? - koks_rs
- @koks_rs if this is a service - Grundy
- Is it just for the server version of Windows? In the usual no more than one, and possibly remote assistant. - Qwertiy ♦
- 2@Qwertiy: And you look at the article on the link. Media Center Extender has not been canceled. Windows has no such internal concept - “active user”. - VladD
- @koks_rs: Well, the TS seems to write “There can be many users on the local machine” - so the question is not about the account under which the program runs, but about all possible. You can put the program in autoload for each of the users, then there will be a copy in each session. - VladD
|
System.Security.Principal.WindowsIdentity.GetCurrent().IsAuthenticated; - no it doesn't work. For verification, I wrote a small code. the meaning of the code is to create a file in the same folder as the program itself and write the value IsAuthenticated there, then run this code and log in to another user, the value is always true - MiXaiL
- the code itself: string path = "MyTest.txt"; System.Timers.Timer timerCurUser = new System.Timers.Timer (15000); timerCurUser.Elapsed + = (sender, argsForTime) => {bool isAus = System.Security.Principal .WindowsIdentity.GetCurrent (). IsAuthenticated; if (! File.Exists (path)) using (FileStream fs = File.Create (path)) {Byte [] info = new UTF8Encoding (true) .GetBytes (isAus.ToString ()); fs.Write (info, 0, info.Length); } else using (System.IO.StreamWriter file = new System.IO.StreamWriter (path, true)) {file.WriteLine (isAus.ToString ()); }}; timerCurUser.Start (); - MiXaiL
|