/// <summary> /// 初始化数据 /// </summary> private void InitData() { string configDir = GetDirectory(); if (!Directory.Exists(configDir)) { Directory.CreateDirectory(configDir); } string baseFilePath = configDir + FileName; if (!File.Exists(baseFilePath)) { var temp = File.Create(baseFilePath); temp.Close(); //并写入空值 string jsonData = string.Empty; //[{"Name":"ArmyKnowledge", //"ConnectionString":"5fGMkdCNq5PYpZ7Hw/EFFBTDw/sfTwmQeQNfOUvzZS2Qr4+OGoxgT1/si/k9vHXwwpTrfhQj6MDGnDIhxRIoiQdm1L1vM3DbvjgaFciMe2RgcIpsZMeBcK0mDH9Qlk290/UKZWLHO1rJc/B9oSEnsutiBpKl8QF0kXOpqMv/ca8=", //"ProviderName":"System.Data.SqlClient"}] string connectStr = string.Empty; connectStr += string.Format(SQL_SERVER_FORMAT, "", "", "", "", ""); jsonData = "[{" + "\"Name\":\"ArmyKnowledge\"," + "\"ConnectionString\":\"" + CryptoApi.AesEncrypt(connectStr) + "\","//需要加密保存 + "\"ProviderName\":\"System.Data.SqlClient\"" + "}]"; File.WriteAllText(baseFilePath, jsonData); } else { string appInfo = File.ReadAllText(baseFilePath); appInfo = appInfo.Substring(1, appInfo.Length - 2); JObject jo = (JObject)JsonConvert.DeserializeObject(appInfo); string connectionStr = CryptoApi.AesDecrypt(jo["ConnectionString"].ToString());//需要解密 DatabaseConect db = AnalysisConnectString(connectionStr); if (db == null) { MessageBox.Show(this, "数据库连接字符串解析失败!"); } else { txtDbName.Text = db.Name; txtDbServer.Text = db.Server; txtUserId.Text = db.UserId; txtPassword.Text = db.Password; } } }