ConfigurationManager 处理两个表单之间的密码
我在一个 .NET Framework 3.5 项目中有两个 Windows 窗体 FormA
和 FormB
。
应用程序主函数(位于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在表单之间共享数据,您只需创建一个具有
public static
属性的类,并使用它来存储您的密码。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.以下内容将起作用 - 即使您像之前在表单中所做的那样加载配置:
在 FormA
和
FormB 中
如果您不介意耦合表单,那么还有其他选项,例如为 FormB 创建一个构造函数,它需要密码作为参数,并
从 FormA 中的
OnClosing()
方法的重写中调用。--
我之前的回答忽略了您不想保留更改的事实。
The following will work - even if you load the configuration as you have done earlier in the form:
in FormA
and
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
from within an override of the
OnClosing()
method in FormA.--
my earlier answer missed the fact that you dont want to persist the changes.