简体   繁体   中英

Session for windows forms application in C#

Is there a session for Windows based applications for C# in order to record the details of log in in and log out for multiple users?

I tried to use declaring static variables, but it is not the same as a session.

There is no concept of session variables in Windows Forms. You can do:

Create a static class that holds the user name and password and any other variables needed across the application.

In your case it would be something like:

public static class LoginInfo
{
    public static string UserID;
}

Now you can access the UserID simply from anywhere in your code:

MessageBox.Show(LogInfo.UserID);

Or set the values after login like:

LogInfo.UserID = TextBox1.Text;

No, there are no session variables in a normal Windows application (the way there is in a web application). If you need logging for a Windows application I agree with the previously written comment to use some logging framework like log4net, NLog or something like that. Even using the Eventlogs can be an option, but I don't recommend it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM