using System;using System.Runtime.InteropServices;using System.Text;using System.IO;namespace iniText{public class textClass{#region INI库/// summary/// 写入INI文件/// /summary/// param namesection节点名称[如[TypeName]]/param/// param namekey键/param/// param nameval值/param/// param namefilepath文件路径/param/// returns/returns[DllImport(kernel32)]public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);///summary/// 读取INI文件/// /summary/// param namesection节点名称/param/// param namekey键/param/// param namedef值/param/// param nameretvalstringbulider对象/param/// param namesize字节大小/param/// param namefilePath文件路径/param/// returns/returns[DllImport(kernel32)]public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);public string strFilePath;string ContentValue(string Section, string key, string path){StringBuilder temp new StringBuilder(1024);GetPrivateProfileString(Section, key, , temp, 1024, path);return temp.ToString();}public void WriteParameter(string Section, string Key, string Value){try{//根据INI文件名设置要写入INI文件的节点名称//此处的节点名称完全可以根据实际需要进行配置WritePrivateProfileString(Section, Key, Value, strFilePath);}catch (Exception ex){throw new Exception(ex.Message, ex);}}public string ReadParameter(string Section, string Key){if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在{string str ContentValue(Section, Key, strFilePath);if (str null || str string.Empty){WriteParameter(Section, Key, 0);return 0;}elsereturn str;}else{return INI文件不存在;}}#endregion#region logpublic void SaveFile(string logpath, string FileName, string str, string TextHeader, string type .txt){string path logpath \\;string newfile path FileName type;StreamWriter streamWriter;// Determine whether the directory exists.if (!Directory.Exists(path)){// Create the directory it does not exist.Directory.CreateDirectory(path);}if (!File.Exists(newfile)){FileStream fs File.Create(newfile);fs.Close();streamWriter File.AppendText(newfile);streamWriter.Write(Encoding.UTF8.GetString(new byte[] { 0xEF, 0xBB, 0xBF }));streamWriter.WriteLine(TextHeader);streamWriter.Flush();streamWriter.Dispose();streamWriter null;}streamWriter File.AppendText(newfile);streamWriter.Write(str \r\n);if (streamWriter ! null){streamWriter.Flush();streamWriter.Dispose();streamWriter null;}}#endregion}}