c# - 在 C# 中使用密码从 Access File 保存和读取 AppSetting
问题描述
我有从文本文件中读取和保存 AppSetting 的类。现在我想将 txt 文件更改为带密码的访问文件。问题是我不知道怎么写。这是我的代码:
public static string HashKey = "hometelaccess";
static readonly string filePath = Application.StartupPath + "/Server.txt";
public static string GetValue(string key)
{
key = key.ToLower();
List<KeyValue> lstKeyValue = GetLstKeyValue();
return lstKeyValue.FirstOrDefault(x => x.Key == key)?.Value.Decrypt(HashKey);
}
private static List<KeyValue> GetLstKeyValue()
{
List<KeyValue> lstKeyValue = new List<KeyValue>();
List<string> lstLine = File.ReadAllLines(filePath).ToList();
foreach (var line in lstLine)
{
string[] strTmp = line.Split(',');
lstKeyValue.Add(new KeyValue { Key = strTmp[0], Value = strTmp[1] });
}
return lstKeyValue;
}
public static void SaveToConfig(string key, string value)
{
key = key.ToLower();
if (!File.Exists(filePath))
File.Create(filePath);
List<KeyValue> lstKeyValue = GetLstKeyValue();
KeyValue keyValue = lstKeyValue.FirstOrDefault(x => x.Key == key);
if (keyValue == null)
lstKeyValue.Add(new KeyValue { Key = key, Value = value.Encrypt(HashKey) });
else keyValue.Value = value.Encrypt(HashKey);
File.WriteAllLines(filePath, lstKeyValue.Select(x => x.Key + "," + x.Value));
}
}
我的问题是我真的不知道如何连接这个有密码的访问文件以及如何从中读取。
解决方案
我终于找到了方法,所以我想我会很好地分享它
public static string HashKey = "hometelaccess";
public static string GetValue(string key)
{
key = key.ToLower();
List<KeyValue> lstKeyValue = GetLstKeyValue();
return lstKeyValue.FirstOrDefault(x => x.Key == key)?.Value.Decrypt(HashKey);
}
private static List<KeyValue> GetLstKeyValue()
{
List<KeyValue> lstKeyValue = new List<KeyValue>();
var conectstring = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Application.StartupPath + "/DataString.mdb"};Jet OLEDB:Database Password =123987741963;";
try
{
using (OleDbConnection conection = new OleDbConnection())
{
using (OleDbCommand ocm = new OleDbCommand())
{
conection.ConnectionString = conectstring;
conection.Open();
ocm.Connection = conection;
OleDbDataAdapter command = new OleDbDataAdapter("select * from [Tbl_Config]", conection);
System.Data.DataTable dt = new System.Data.DataTable();
command.Fill(dt);
foreach (DataRow row in dt.Rows)
{
lstKeyValue.Add(new KeyValue { Key = row["Key"].ToString(), Value = row["Value"].ToString() });
}
conection.Close();
}
}
}
catch (Exception e)
{
}
return lstKeyValue;
}
public static void SaveToConfig(string key, string value)
{
key = key.ToLower();
if (!File.Exists(filePath))
File.Create(filePath);
List<KeyValue> lstKeyValue = GetLstKeyValue();
var conectstring = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Application.StartupPath + "/Datastring.mdb"};Jet OLEDB:Database Password =123987741963;";
try
{
using (OleDbConnection conection = new OleDbConnection())
{
using (OleDbCommand ocm = new OleDbCommand())
{
conection.ConnectionString = conectstring;
ocm.Connection = conection;
conection.Open();
KeyValue keyValue = lstKeyValue.FirstOrDefault(x => x.Key == key);
if (keyValue == null)
{
var x= ocm.CommandText = "Insert Into [Tbl_Config] ([Key] , [Value]) VALUES(?,?)";
ocm.Parameters.AddWithValue("key", key);
ocm.Parameters.AddWithValue("Value", value.Encrypt(HashKey));
ocm.ExecuteNonQuery();
}
else
{
ocm.CommandText = "UPDATE [Tbl_Config] SET [Value]=? where [Key]=?";
ocm.Parameters.Add("@Value", OleDbType.LongVarWChar).Value = value.Encrypt(HashKey);
ocm.Parameters.Add("@key", OleDbType.LongVarWChar).Value = key;
ocm.ExecuteNonQuery();
}
conection.Close();
}
}
}
catch (Exception e)
{
}
}
推荐阅读
- java - 大海捞针(将两个对象与 .equals 进行比较时出错)
- java - 解析特定格式的 JSON 文件
- javascript - 我在我的 npm 包中的哪里导出函数以便开发人员可以导入它们?
- perl - Perl - 管道 gunzip 输出到 File::ReadBackwards
- excel - 如何粘贴到最后填充的行而不包括第一行(标题)?
- git - git pull vs git pull --rebase 尝试 git push 时
- javascript - 如何从提交事件处理程序向对象数组添加新元素
- php - 我无法获得完整的 slug/post 路线
- javascript - 使用 Prisma 生成代码的变量错误
- r - 是否可以缓存“lm()”矩阵以适应新数据?