| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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");
- }
- /// <summary>
- /// 获取本机的MAC地址
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 将即时日志保存入日志文件
- /// </summary>
- 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)
- {
- }
- }
- }
- }
|