ConfigurationManager 处理两个表单之间的密码

发布于 2025-01-06 11:24:30 字数 815 浏览 1 评论 0原文

我在一个 .NET Framework 3.5 项目中有两个 Windows 窗体 FormAFormB

应用程序主函数(位于 Programm.cs 中)在 FormA 停止后启动 FormB

Application.Run(new FormA());
Application.Run(new FormB());

FormA 执行以下操作以引入 FormB code> AppSetting Password

System.Configuration.Configuration config =
          ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Add("password", textBoxPassword.Text);

,仅此而已:) 我不想将密码保存在 app.config 文件中。

好的,应用程序启动,FormA() 出现并关闭,FormB() 出现,尝试读取 ConfigurationSettings.AppSettings["Password"];,其值为 NULL

我如何让这个东西给我带来我在FormA中设置的值而不是NULL

I've got two Windows Forms FormA and FormB in one .NET Framework 3.5 project.

The Applications Main function(placed in Programm.cs) starts FormB after FormA stops:

Application.Run(new FormA());
Application.Run(new FormB());

FormA does the following to bring FormB the AppSetting Password

System.Configuration.Configuration config =
          ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings.Add("password", textBoxPassword.Text);

and nothing else :) I do not want to save the password in the app.config file.

Okay, Application starts, FormA() comes up and gets closed, FormB() comes up, tries to read ConfigurationSettings.AppSettings["Password"]; which is NULL.

How do I make this thing bringing me the Value I set in FormA instead of NULL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2025-01-13 11:24:30

如果您想在表单之间共享数据,您只需创建一个具有 public static 属性的类,并使用它来存储您的密码。

public static class SharedData
{
    public static string Password { set; get; }

    // ... other shared properties

}

If you want to share data between forms you can simply create a class with a public static property and use it to store your password.

public static class SharedData
{
    public static string Password { set; get; }

    // ... other shared properties

}
策马西风 2025-01-13 11:24:30

以下内容将起作用 - 即使您像之前在表单中所做的那样加载配置:

ConfigurationSettings.AppSettings["password"] = textboxPassword.Text;

在 FormA

string pwd = ConfigurationSettins.AppSettings["password"];

FormB 中

如果您不介意耦合表单,那么还有其他选项,例如为 FormB 创建一个构造函数,它需要密码作为参数,并

new FormB(this.textboxPassword.Text).Show(); 

从 FormA 中的 OnClosing() 方法的重写中调用。

--
我之前的回答忽略了您不想保留更改的事实。

The following will work - even if you load the configuration as you have done earlier in the form:

ConfigurationSettings.AppSettings["password"] = textboxPassword.Text;

in FormA

and

string pwd = ConfigurationSettins.AppSettings["password"];

in FormB

If you don't mind coupling your forms then there are other options, for example create a constructor for FormB that takes a password as a parameter, and call

new FormB(this.textboxPassword.Text).Show(); 

from within an override of the OnClosing() method in FormA.

--
my earlier answer missed the fact that you dont want to persist the changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文