网站首页 > 开源技术 正文
常见的保存方式包括使用 .config 文件、Registry、.ini 文件、JSON 文件、XML 文件、SQLite 数据库等。
1. 使用 .config 文件 (app.config 或 web.config)
// 读取设置
string keyFieldName = Properties.Settings.Default.KeyFieldName;
// 更新并保存设置
Properties.Settings.Default.KeyFieldName = "新姓名";
Properties.Settings.Default.Save();
2. 使用 Windows 注册表 (Registry)
using Microsoft.Win32;
// 保存值到注册表
RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\MyApp");
key.SetValue("KeyFieldName", "新姓名");
key.Close();
// 从注册表读取值
key = Registry.CurrentUser.OpenSubKey(@"Software\MyApp");
string keyFieldName = key.GetValue("KeyFieldName", "默认姓名").ToString();
key.Close();
3. 使用 .ini 文件
使用 P/Invoke 调用 Windows API 来处理 .ini 文件。
using System.Runtime.InteropServices;
using System.Text;
public class IniFile
{
public string Path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public IniFile(string iniPath)
{
Path = iniPath;
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, Path);
}
public string Read(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString(section, key, "", temp, 255, Path);
return temp.ToString();
}
}
// 使用示例
IniFile ini = new IniFile(@"C:\path\to\yourfile.ini");
// 写入值
ini.Write("Settings", "KeyFieldName", "新姓名");
// 读取值
string keyFieldName = ini.Read("Settings", "KeyFieldName");
4. 使用 JSON 文件
使用 Newtonsoft.Json 或 System.Text.Json 库来处理 JSON 文件。
using System.IO;
using Newtonsoft.Json;
public class Settings
{
public string KeyFieldName { get; set; }
}
// 保存设置
Settings settings = new Settings { KeyFieldName = "新姓名" };
File.WriteAllText(@"C:\path\to\settings.json", JsonConvert.SerializeObject(settings));
// 读取设置
Settings loadedSettings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(@"C:\path\to\settings.json"));
string keyFieldName = loadedSettings.KeyFieldName;
5. 使用 XML 文件
using System.IO;
using System.Xml.Serialization;
public class Settings
{
public string KeyFieldName { get; set; }
}
// 保存设置到 XML 文件
Settings settings = new Settings { KeyFieldName = "新姓名" };
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
using (TextWriter writer = new StreamWriter(@"C:\path\to\settings.xml"))
{
serializer.Serialize(writer, settings);
}
// 从 XML 文件读取设置
XmlSerializer deserializer = new XmlSerializer(typeof(Settings));
using (TextReader reader = new StreamReader(@"C:\path\to\settings.xml"))
{
Settings loadedSettings = (Settings)deserializer.Deserialize(reader);
string keyFieldName = loadedSettings.KeyFieldName;
}
6. 使用 SQLite 数据库
使用 System.Data.SQLite 库来处理 SQLite 数据库。
using System.Data.SQLite;
// 创建 SQLite 数据库连接
using (var connection = new SQLiteConnection("Data Source=settings.db"))
{
connection.Open();
// 创建表
string createTableQuery = "CREATE TABLE IF NOT EXISTS Settings (KeyFieldName TEXT)";
SQLiteCommand command = new SQLiteCommand(createTableQuery, connection);
command.ExecuteNonQuery();
// 插入数据
string insertQuery = "INSERT INTO Settings (KeyFieldName) VALUES (@KeyFieldName)";
command = new SQLiteCommand(insertQuery, connection);
command.Parameters.AddWithValue("@KeyFieldName", "新姓名");
command.ExecuteNonQuery();
// 读取数据
string selectQuery = "SELECT KeyFieldName FROM Settings LIMIT 1";
command = new SQLiteCommand(selectQuery, connection);
string keyFieldName = command.ExecuteScalar().ToString();
}
7. 使用 Environment Variables 环境变量
// 设置环境变量
Environment.SetEnvironmentVariable("KeyFieldName", "新姓名", EnvironmentVariableTarget.User);
// 读取环境变量
string keyFieldName = Environment.GetEnvironmentVariable("KeyFieldName", EnvironmentVariableTarget.User);
猜你喜欢
- 2024-11-12 开源云盘利器:Nextcloud 21私有云盘搭建
- 2024-11-12 三步完成测试监控系统搭建(监控摄像机测试方案)
- 2024-11-12 移动开发工具Xamarin.Android更新至6.1.2
- 2024-11-12 .NET 6 Minimal API程序中使用EF Core访问SQLite数据库实例教程
- 2024-11-12 分享一款PG管理工具--pgadmin,一个类似PLSQL功能的工具
- 2024-11-12 关于Android系统框架的核心组成部分,你知道多少?
- 2024-11-12 「asp.net core 系列」8实战之利用EF Core 实现数据操作层
- 2024-11-12 Clace和sqlite-fs:使用SQLite作为文件系统替代方案
- 2024-11-12 从失败到成功:如何在C#中删除SQLite数据库文件
- 2024-11-12 NetCore使用Sqlite数据库入门基础
你 发表评论:
欢迎- 03-19基于layui+springcloud的企业级微服务框架
- 03-19开箱即用的前端开发模板,扩展Layui原生UI样式,集成第三方组件
- 03-19SpringMVC +Spring +Mybatis + Layui通用后台管理系统OneManageV2.1
- 03-19SpringBoot+LayUI后台管理系统开发脚手架
- 03-19layui下拉菜单form.render局部刷新方法亲测有效
- 03-19Layui 遇到的坑(记录贴)(layui chm)
- 03-19基于ASP.NET MVC + Layui的通用后台开发框架
- 03-19LayUi自定义模块的定义与使用(layui自定义表格)
- 最近发表
-
- 基于layui+springcloud的企业级微服务框架
- 开箱即用的前端开发模板,扩展Layui原生UI样式,集成第三方组件
- SpringMVC +Spring +Mybatis + Layui通用后台管理系统OneManageV2.1
- SpringBoot+LayUI后台管理系统开发脚手架
- layui下拉菜单form.render局部刷新方法亲测有效
- Layui 遇到的坑(记录贴)(layui chm)
- 基于ASP.NET MVC + Layui的通用后台开发框架
- LayUi自定义模块的定义与使用(layui自定义表格)
- Layui 2.9.11正式发布(layui2.6)
- Layui 2.9.13正式发布(layui2.6)
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)