C# ini/文件/目录 操作方法
#region API函数声明[DllImport("kernel32")]//返回0表示失败,非0为成功private static extern long WritePrivateProfileString(string section, string key,string val, string filePath);[DllImport("kernel32")]//返回取得字符串缓冲区的长度private static extern long GetPrivateProfileString(string section, string key,string def, StringBuilder retVal, int size, string filePath);#endregion#region ini文件操作//读Ini文件public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath){if (File.Exists(iniFilePath)){StringBuilder temp = new StringBuilder(1024);GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);return temp.ToString();}else{return String.Empty;}}//写Ini文件public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath){if (!File.Exists(iniFilePath)){//如果不存在该文件,创建它Utils.WriteFile(iniFilePath, "");}long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);if (OpStation == 0){return false;}else{return true;}}#endregion#region 本地文件操作//读文件public static string ReadFile(string path) {if (File.Exists(path)){//如果文件存在string test = File.ReadAllText(path, Encoding.Default);return test;}else {return String.Empty;}}//写文件public static void WriteFile(string path, string content){//增加,如果没有该文件,创建它再增加File.AppendAllText(path, content);}//创建目录public static void WriteDir(string path){// Check to see if a directory existsbool dirExists = Directory.Exists(path);//目录不存在if (!dirExists)Directory.CreateDirectory(path);}//删除文件public static void DelFile(string path){File.Delete(path);}#endregion