首页 > 解决方案 > 在 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));
    }
}

我的问题是我真的不知道如何连接这个有密码的访问文件以及如何从中读取。

标签: c#ms-accessappsettings

解决方案


我终于找到了方法,所以我想我会很好地分享它

 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)
        {

        }

    }

推荐阅读