❶ 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配置文件!認為它不是一個好的語言。
如果你對常見的配置文件有哪幾種格式不熟悉,就去黑馬程序員官網視頻庫看免費視頻。
❸ 綾葷殑鏋勯犲嚱鏁伴兘鏈夊摢浜涚被鐨勬瀯閫犲嚱鏁伴兘鏈夊摢浜
綾葷殑璁捐℃濇兂錛
鏋勯犲嚱鏁幫紝鍙浠ョ粰鐐硅祴鍒濆肩殑鏋勯犲嚱鏁般
鐐圭殑鐩稿叧鍙傛暟浣滀負鎴愬憳鍙橀噺錛屽傛í鍧愭爣錛岀旱鍧愭爣銆
璁捐℃垚鍛樺嚱鏁幫紝濡傚彇寰楀潗鏍囧肩殑鍑芥暟錛屾ょ偣鍜屽師鐐逛箣闂寸殑璺濈匯