❶ C# 鏁版嵁搴撹繛鎺ヨ剧疆淇濆瓨涓 ini鏂囦欢闂棰
1.璇诲彇ini鏂囦欢閿鍊煎圭殑绫伙紝杩欐牱鍐
class ConfigureFile
{
public string ConfigureFilePath; //INI鏂囦欢鍚
//澹版槑璇诲啓INI鏂囦欢鐨凙PI鍑芥暟
[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 ConfigureFile()
{
//
// TODO: 鍦ㄦゅ勬坊鍔犳瀯閫犲嚱鏁伴昏緫
//
}
//绫荤殑鏋勯犲嚱鏁帮紝浼犻扞NI鏂囦欢鍚
public ConfigureFile(string ConfigureFilePath)
{
this.ConfigureFilePath = ConfigureFilePath;
}
//鍐橧NI鏂囦欢
public void WriteConfigureFileValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.ConfigureFilePath);
}
//璇诲彇INI鏂囦欢鎸囧畾
public string ReadConfigureFileValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.ConfigureFilePath);
Console.Write(temp.ToString());
if (temp.ToString() == null ||temp.ToString() =="")
{
throw new Exception("Profile.ini璇诲彇閰嶇疆鏂囦欢涓虹┖.");
}
return temp.ToString();
}
}
2.澶勭悊璇诲彇鐨勯敭鍊煎硅繖鏍峰啓
/// <summary>
/// 杩斿洖Profile.ini閰嶇疆鍙傛暟
/// </summary>
public class Profile
{
private static ConfigureFile ReadConfigureFile = new ConfigureFile(".\\Profile.ini");
/// <summary>
/// 杩斿洖鏁版嵁搴撴湇鍔″悕
/// </summary>
/// <returns></returns>
public static string getServerName()
{
//瀵逛簬浣犵殑闂棰 杩欎釜杩斿洖鍊煎氨鏄痭or 鍦╥ni鏂囦欢閲屽氨鏄疭erverName=nor
return ReadConfigureFile.ReadConfigureFileValue("DataBase", "ServerName");
}
/// <summary>
/// 杩斿洖鐧婚檰璐︽埛
/// </summary>
/// <returns></returns>
public static string getLogId()
{
//瀵逛簬浣犵殑闂棰 杩斿洖鍊煎氨鏄 sa 鍦╥ni閲屽氨鏄疞ogId=sa
return ReadConfigureFile.ReadConfigureFileValue("DataBase", "LogId");
}
/// <summary>
/// 杩斿洖鐧婚檰瀵嗙爜
/// </summary>
/// <returns></returns>
public static string getPassWord()
{
//瀵逛簬浣犵殑闂棰樿繑鍥炲煎氨鏄**** 鍦╥ni閲屽氨鏄疨assWord=*****锛***鏄鍔犲瘑鍚庣殑瀵嗙爜 瀵嗘枃褰㈠紡锛
return ReadConfigureFile.ReadConfigureFileValue("DataBase", "PassWord");
}
}
鎴戞妸璇籭ni鏂囦欢 鍜岃诲彇ini鏂囦欢涓鐨刱ey/value瀵瑰垎寮鍐 杩欐牱鏄涓轰簡璁╀綘鑳芥洿鐏垫椿鐨勫幓娣诲姞ini涓鐨刱ey/value瀵 褰撲綘娣诲姞key.value瀵规椂 鍙闇瑕佸湪Profile绫讳腑娣诲姞鐩稿簲鐨刧et鏂规硶灏卞彲浠ヤ簡 鏂逛究淇鏀
ini鏂囦欢涓鐨勯敭鍊煎瑰啓鎴愯繖鏍
[DATABASE]
ServerName='nor'
LogId='sa'
PassWord='****'锛****鏄瀵嗘枃褰㈠紡锛
3.杩欐槸鍔犲瘑浣犲瘑鐮佺殑绫 鎴戠敤MD5閰嶅悎鍔犵洂鍊艰繘琛屽姞瀵 杩欐牱灏卞緢瀹夊叏
public class MD5Encrypt
{
/// <summary>
/// 灏嗘槑鏂囩敤MD5鍔犲瘑
/// </summary>
/// <param name="strPwd">闇瑕佸姞瀵嗙殑鏄庢枃</param>
/// <param name="strKey">鐢ㄤ簬鍔犲瘑鐨凨ey(鍔犵洂鍊)</param>
/// <returns>鍔犲瘑鍚庡緱鍒扮殑MD5鍊</returns>
public static string ToEncodingByMD5(string strPwd, string strKey)
{
string key = strKey.Trim();
string strEnPwd = string.Empty;
if (key.Length == 0)
{
key = "lwk";//榛樿ゅ姞鐩愬 浣犺嚜宸卞畾涔夊姞鐩愬硷紝瑕侀獙璇佺櫥褰曞瘑鐮佹槸鍚︽g‘ 闇瑕佷紶杩涘拰淇濆瓨鍏跺姞瀵嗘椂 //鐩稿悓鐨勫姞鐩愬 鐒跺悗瀵规瘮浠栦滑鐨勫瘑鏂囨槸涓嶆槸鐩稿悓
}
byte[] Original = Encoding.Default.GetBytes(strPwd);
byte[] SaltValue = Encoding.Default.GetBytes(key);
byte[] ToKey = new byte[Original.Length + SaltValue.Length];
Original.CopyTo(ToKey, 0);
SaltValue.CopyTo(ToKey, Original.Length);
MD5 st = MD5.Create();
byte[] SaltPWD = st.ComputeHash(ToKey);
byte[] PWD = new byte[SaltPWD.Length + SaltValue.Length];
SaltPWD.CopyTo(PWD, 0);
SaltValue.CopyTo(PWD, SaltPWD.Length);
strEnPwd = Convert.ToBase64String(PWD);
return strEnPwd;
}
}
❷ SpringBoot的配置文件有哪几种格式
SpringBoot中的配置文件来主要有三种格式,自properties、yaml、和xml方式。
- 其中properties格式配置文件后缀是.properties,配置项为:server.port = 9090
- yaml格式配置文件后缀是.yml,配置项是:server.port: 9090
在SpringBoot中,使用最广泛的配置文件是yaml,yaml之所以流行,除了他配置语法精简之外,还因为yaml是一个跨编程语言的配置文件。
在SpringBoot中,除了yaml之外,properties也比较常用,但是XML几乎不用,看得出来Spring团队非常痛恨XML配置文件!认为它不是一个好的语言。
如果你对常见的配置文件有哪几种格式不熟悉,就去黑马程序员官网视频库看免费视频。
❸ 绫荤殑鏋勯犲嚱鏁伴兘鏈夊摢浜涚被鐨勬瀯閫犲嚱鏁伴兘鏈夊摢浜
绫荤殑璁捐℃濇兂锛
鏋勯犲嚱鏁帮紝鍙浠ョ粰鐐硅祴鍒濆肩殑鏋勯犲嚱鏁般
鐐圭殑鐩稿叧鍙傛暟浣滀负鎴愬憳鍙橀噺锛屽傛í鍧愭爣锛岀旱鍧愭爣銆
璁捐℃垚鍛樺嚱鏁帮紝濡傚彇寰楀潗鏍囧肩殑鍑芥暟锛屾ょ偣鍜屽師鐐逛箣闂寸殑璺濈汇