【问题标题】:Save a value of variable in a standalone application [duplicate]在独立应用程序中保存变量值[重复]
【发布时间】:2016-04-01 11:41:13
【问题描述】:

我有一个简单的字符串变量,我想在启动应用程序时保存该值,该应用程序是独立的,我不想使用注册表。 我试图将字符串作为设置,但它保存在 app.config 文件中。

我可以在自我应用程序中保存变量吗?

【问题讨论】:

  • 您无法将其保存到 EXE 文件中。您需要将其保存在其他文件中,例如“app.config”、文本文件或某种数据库。

标签: c# vb.net visual-studio-2015


【解决方案1】:

我通常做的是将字符串保存在这样的文本文件中:

var stringToSave = "MyString";
var filePath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData) + "\\YourApplicationName\\data.dat";

using (StreamWriter sw = new StreamWriter (filePath)) {
    sw.WriteLine (stringToSave);
}

我在这里所做的基本上是获取 AppData 的目录并在其中写入一个名为 data.dat 的文件。它应该是不言自明的。

【讨论】:

  • 感谢参与
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多