using System.Text; using System.Runtime.InteropServices; using System.Management; using System; using System.Threading.Tasks; using System.IO; namespace Ini { public class IniFile { public string path; //INI文件名 111 public string HISIP; public string HISURL; public string HISauthorization; [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); //声明读写INI文件的API函数 public IniFile() { try { path = System.IO.Directory.GetCurrentDirectory() + @"\INSUConfigure.ini"; if (System.IO.File.Exists(path) == false) { path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//INSUConfigure.ini"; } } catch (System.Exception ex) { path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//INSUConfigure.ini"; } } //类的构造函数,传递INI文件名 //写INI文件 public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.path); } //读取INI文件指定 public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); return temp.ToString(); } public string IniReadLogPath() { return IniReadValue("LOG", "path"); } /// /// 获取本机的MAC地址 /// /// public string GetMACString() { ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection mo = mAdapter.GetInstances(); foreach (ManagementBaseObject m in mo) { if ((bool)m["IpEnabled"] == true) { return m["MacAddress"].ToString(); } } mo.Dispose(); return null; } } class FileClass { //创建文件夹 //参数:path 文件夹路径 public bool CreateFolder(string path) { try { if (Directory.Exists(path)) { return true; } if (!Directory.Exists(path.Substring(0, path.LastIndexOf("\\")))) { //若路径中无“\”则表示路径错误 return false; } else { //创建文件夹 DirectoryInfo dirInfo = Directory.CreateDirectory(path); return true; } } catch (Exception ex) { return false; } } //创建文件 //参数:path 文件路径 public void CreateFile(string path) { try { if (CreateFolder(path.Substring(0, path.LastIndexOf("\\")))) { if (!File.Exists(path)) { FileStream fs = File.Create(path); fs.Close(); } } } catch (Exception ex) { return; } } //删除文件 //参数:path 文件夹路径 public void DeleteFile(string path) { try { if (!File.Exists(path)) { return; } else { File.Delete(path); } } catch (Exception ex) { return; } } //写文件 //参数:path 文件夹路径 、content要写的内容 public void WriteFile(string path, string content) { try { if (!File.Exists(path)) { CreateFile(path); } FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(content); sw.Close(); } catch (Exception ex) { return; } } /// /// 将即时日志保存入日志文件 /// public void WriteLogFile( string content) { string directoryPath = System.AppDomain.CurrentDomain.BaseDirectory + "YBLog"; if (!Directory.Exists(directoryPath)) { CreateFolder(directoryPath); } try { //写入新的文件 string filePath = directoryPath + "\\" + DateTime.Now.Date.ToString("yyyyMMdd") + ".log"; FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(content); sw.Close(); fs.Close(); } catch (Exception ex) { } } } }