C# 怎么保留一个变量的值

sffofn 2012-05-16 10:49:00
在一个项目里的windowsForm 设置了一个变量值 ,在其主项目里调用这个窗体 怎么保持我上一次在这个窗体里设置的这个变量的值 急!
...全文
737 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Josslin025 2014-02-11
  • 打赏
  • 举报
回复
引用 4 楼 mabaolin 的回复:
如果是重启后的保持,需要用ls得方法。就是写config文件中。 使用ConfigurationManager类 读写配置文件app.config,以下为代码: view plaincopy to clipboardprint?using System; using System.Configuration; static class Program { static void Main() { showConfig(); UpdateAppSettings(); showConfig(); Console.ReadKey(true); } private static void showConfig() { string = ConfigurationManager.AppSettings["Directory"]; Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "/n"); } /// <summary> /// UpdateAppSettings /// </summary> public static void UpdateAppSettings() { // Get the configuration file. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("config.FIlePath: " + config.FilePath + "/n"); config.AppSettings.Settings["Directory"].Value = "tset"; // Save the configuration file. config.AppSettings.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. ConfigurationManager.RefreshSection("appSettings"); } using System; using System.Configuration; static class Program { static void Main() { showConfig(); UpdateAppSettings(); showConfig(); Console.ReadKey(true); } private static void showConfig() { string = ConfigurationManager.AppSettings["Directory"]; Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "/n"); } /// <summary> /// UpdateAppSettings /// </summary> public static void UpdateAppSettings() { // Get the configuration file. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("config.FIlePath: " + config.FilePath + "/n"); config.AppSettings.Settings["Directory"].Value = "tset"; // Save the configuration file. config.AppSettings.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. ConfigurationManager.RefreshSection("appSettings"); } app.config内容: view plaincopy to clipboardprint?<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Directory" value="C:/Documents and Settings"/> </appSettings> </configuration> <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Directory" value="C:/Documents and Settings"/> </appSettings> </configuration> 代码结果:app.config只能作为初始化的定义,工程生成后运行程序集名称.exe 修改生成后的 程序集名称.exe.Config文件 一开始调试时看到控制结果是想要的结果,但看app.config配置文件内容没变(vs2008 F5调试模式下是修改 程序集名称.vshost.exe.config配置文件)还以为是代码有问题,网上搜,也有人碰过到此现像,原来是自己没有理解到MSDN的说明。(还是有文化差异啊) 如: Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("config.FIlePath: " + config.FilePath + "/n"); 查看config.filePath值,即了解明白了。
写config文件是可以实现的。
westfly5 2012-05-16
  • 打赏
  • 举报
回复 1
public string readonly string a=string.Empty
jsj30651 2012-05-16
  • 打赏
  • 举报
回复
可以保存在注册表或者临时文件中
surlew 2012-05-16
  • 打赏
  • 举报
回复
晕,选择才看清楚问题,这个可以在文件里面保存,也可以在数据库保存,都可以的,看你自己选择了
sffofn 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
楼主是说窗口关了再打开时显示上一次的内容吧。那要么把内容写入数据库表中。要么保存到文件中。如**.ini文件中以key-value形式存放。
userID=myIdValue
userPWD=myPWDValue
[/Quote]

嗯 因为这个项目是多人登陆的, 还是写数据库好一点吧 是吗
surlew 2012-05-16
  • 打赏
  • 举报
回复
你可以在一个public类里面建一个public static string _value;
然后用这个_value来保存你要存储的值
test2050 2012-05-16
  • 打赏
  • 举报
回复
楼主是说窗口关了再打开时显示上一次的内容吧。那要么把内容写入数据库表中。要么保存到文件中。如**.ini文件中以key-value形式存放。
userID=myIdValue
userPWD=myPWDValue
mabaolin 2012-05-16
  • 打赏
  • 举报
回复
如果是重启后的保持,需要用ls得方法。就是写config文件中。

使用ConfigurationManager类 读写配置文件app.config,以下为代码:

view plaincopy to clipboardprint?using System;
using System.Configuration;

static class Program
{
static void Main()
{
showConfig();
UpdateAppSettings();
showConfig();

Console.ReadKey(true);
}

private static void showConfig()
{
string = ConfigurationManager.AppSettings["Directory"];
Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "/n");
}

/// <summary>
/// UpdateAppSettings
/// </summary>
public static void UpdateAppSettings()
{
// Get the configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine("config.FIlePath: " + config.FilePath + "/n");
config.AppSettings.Settings["Directory"].Value = "tset";

// Save the configuration file.
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
using System;
using System.Configuration;

static class Program
{
static void Main()
{
showConfig();
UpdateAppSettings();
showConfig();

Console.ReadKey(true);
}

private static void showConfig()
{
string = ConfigurationManager.AppSettings["Directory"];
Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "/n");
}

/// <summary>
/// UpdateAppSettings
/// </summary>
public static void UpdateAppSettings()
{
// Get the configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine("config.FIlePath: " + config.FilePath + "/n");
config.AppSettings.Settings["Directory"].Value = "tset";

// Save the configuration file.
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
app.config内容:

view plaincopy to clipboardprint?<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Directory" value="C:/Documents and Settings"/>
</appSettings>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Directory" value="C:/Documents and Settings"/>
</appSettings>
</configuration>



代码结果:app.config只能作为初始化的定义,工程生成后运行程序集名称.exe 修改生成后的 程序集名称.exe.Config文件

一开始调试时看到控制结果是想要的结果,但看app.config配置文件内容没变(vs2008 F5调试模式下是修改 程序集名称.vshost.exe.config配置文件)还以为是代码有问题,网上搜,也有人碰过到此现像,原来是自己没有理解到MSDN的说明。(还是有文化差异啊)

如: Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine("config.FIlePath: " + config.FilePath + "/n");

查看config.filePath值,即了解明白了。

rybin_1987 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

可以将这个值作为一个全局变量,不一定非得在窗体中,还可以用settings
[/Quote]
+1
bdmh 2012-05-16
  • 打赏
  • 举报
回复
可以将这个值作为一个全局变量,不一定非得在窗体中,还可以用settings
sanlonezh 2012-05-16
  • 打赏
  • 举报
回复
public static readonly string a=string.Empty;

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧