C# 中 windows 窗体应用程序的会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14599127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:23:41  来源:igfitidea点击:

Session for windows forms application in C#

c#winformssession

提问by Tsuna Sawada

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?

是否有用于 C# 的基于 Windows 的应用程序的会话,以便记录多个用户的登录和注销的详细信息?

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

我尝试使用声明静态变量,但它与会话不同。

采纳答案by sajanyamaha

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

Windows 窗体中没有会话变量的概念。你可以做:

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:

现在您可以简单地从代码中的任何位置访问 UserID:

MessageBox.Show(LogInfo.UserID);

Or set the values after login like:

或在登录后设置值,如:

LogInfo.UserID = TextBox1.Text;

回答by Sandman

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.

不,在普通的 Windows 应用程序中没有会话变量(就像在 Web 应用程序中那样)。如果您需要为 Windows 应用程序进行日志记录,我同意之前写的评论,以使用一些日志记录框架,如 log4net、NLog 或类似的东西。即使使用事件日志也可以是一种选择,但我不推荐它。