0

I need to get the Windows login time for the current user.

I am using a Windows .NET Form. Nothing I've found works. The time that I end up getting is the LAST login time and not the latest login time which makes no sense. Seems to me that Microsoft broke this function but what do i know.

I have already tried all methods on this page and still not the right times. I started a new thread as to not resurrect a dead thread but I may be wrong for doing this. Sorry if that is the case.

2 Answers2

1

I can't comment yet so I have to propose this as an answer.

Have you tried running the tool wiser in the manner suggested here?

Run Command Prompt Commands

Inside here you can capture the output and parse it.

    private List<string> RunCommand(string exeName, string args, string folder)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo(exeName);

        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.WorkingDirectory = folder;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.Arguments = args;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        var results = new List<string>();
        while (!process.StandardOutput.EndOfStream)
        {
            results.Add(process.StandardOutput.ReadLine());
        }

        return results;
    }

Above is the code I use.

Apologies for formatting as I am doing this on my phone

1

The current user is

  System.Security.Principal.WindowsIdentity.GetCurrent()

But there is no Login-Time stored, and your approach sounds strange.

If someone starts your Application 5 days after Windows was started, you want to logout the user from windows, because he logged in more then 45min ago ? I hope I misunderstand this.

You just have to track the time your application is running. It's a single-user application, you can shut down your application at any time, if you like.

Holger
  • 2,446
  • 1
  • 14
  • 13
  • Yes, you talk about the windows login, somebody logs in to windows, and 5 days later your start your application. You do not talk about a web application, where you first start the application and then you login. You first login, and after that you start the application. You mix-up Login to Windows and login to a single application. You cannot start anything at login to windows, cause your WinForms application is not running. – Holger Jan 01 '20 at 18:18
  • @xCONFLiCTiONx Your question did not include the word Service. A Windows-Service is the correct approach, a Service runs before the user logs in. But a Service is never a WinForms-application, it has no user-interface. For my understanding you wanted to use a user-application like Microsoft Word to log off from Windows - that was just confusing. Windows-Login is always the first thing a user does - starting an application always comes later, endless time later, can be days later. – Holger Jan 02 '20 at 06:57