我必须永久存储上传的文件版本和以前的文件名和版本,此外,我需要更新和检索该信息,但我没有为此应用程序使用任何数据库。是否可以将数据保存在 Web.Config 文件中并能够更新?
3 回答
不要将它保存在 Web.Config 中。更新 web.config 会导致应用程序池回收,如果您愿意,可以将其存储在一些 XML 文件中。不太确定为什么要为此避免使用数据库,但您甚至可以将其存储在基于文件的数据库中,例如SQLite
如果您愿意,您可以将数据保存在 web.config 中,但我不建议这样做。但是如果它是一个小应用程序并且并不真正关心用户的会话状态/应用程序池,那么我不明白为什么不这样做。
<configuration>
<appSettings>
<add key="Version" value="2.0.0.0" />
</appSettings>
</configuration>
然后您可以按如下方式检索数据
ConfigurationManager.AppSettings["Version"]
但是,您可以创建自己的xml文件,然后在您的代码中检索它,如下所示
xml
<ApplicationConfig>
<Version>2.0.0.0</Version>
</ApplicationConfig>
然后在你的代码中
if (File.Exists(configFile))
{
var xml = XDocument.Load(configFile);
if (xml.Root != null)
{
var version = xml.Root.Elements("ApplicationConfig").Elements("Version").Value
}
}
将数据存储在文件中是一种方法。有几点需要注意:
- 使用 App_Data 子文件夹,但还要确保您的应用程序池身份在此处具有写入权限。
- 使用线程安全的方法来写入和读取该文件(例如全局锁)。
Alternatively you could write the value in the registry under HKEY_CURRENT_USER
. This approach would not require setting permissions on the file system which sometime is not possible.
Just in case the information you store is unique to the user you might want to use ASP.NET Profile. Since you don't want to use database you can use a custom provider that stores the data in XML files.