非常有用常用的技术点一——WinForm

1、获取PC机的名称:

using System.Net;

//获取PC机的名称
string StrPC = Dns.GetHostName().ToString();

2、输入log到日志文件中

/// 
/// 将log写入日志文件
/// 
/// 要写入的log
/// log文件名
public static void WriteLine(string strLog, string _filename)
{
	// 日志文件名称:log_20090101.log
	string strFileName = _filename + System.DateTime.Now.ToString("yyyyMMdd") + ".log";
	if (strLog != "")
	{
		// 相对路径:根目录log文件夹下,如果目标文件夹不存在则创建一个
        string strPath = Application.StartupPath + "\\log";
        if (!Directory.Exists(strPath)) { System.IO.Directory.CreateDirectory(strPath); }
        // 文件名
        strPath = strPath + "\\" + strFileName;
        // 写日志记录
        try
        {
            // 如果文件存在则追加记录,如果文件不存在则创建并写入记录
            System.IO.StreamWriter objSw = new StreamWriter(strPath, true);
            objSw.WriteLine(strLog);
            objSw.Flush();
            objSw.Close();
        }
        catch { }
    }
}

public static void LogForm(string strMsg)
{
      WriteLine("[" + DateTime.Now.ToString() + "]" + strMsg, "dvi_From");
}

3、读写INI文件

using System.IO;
using System.Runtime.InteropServices;
using System.Text;

    /// 
    /// iniClass 的摘要说明。
    /// 
    // TODO: 在此处添加构造函数逻辑
    public class IniClass
    {
        public string inipath;
        /// 
        /// 写文件
        /// 
        /// 写入文件所属的区域
        /// 键
        /// 值
        /// 文件路径
        /// 
        [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 IniClass(string INIPath)
        {
            inipath = INIPath;
        }
        /// 
        /// 写入INI文件
        /// 
        /// 项目名称(如 [TypeName] )
        /// 键
        /// 值
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }
        /// 
        /// 读出INI文件
        /// 
        /// 项目名称(如 [TypeName] )
        /// 键
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
            return temp.ToString().Trim();
        }
        /// 
        /// 读整数
        /// 
        /// 
        /// 
        /// 
        public int IniReadInt(string section, string key)
        {
            int itInt = 0;
            StringBuilder temp = new StringBuilder(500);
            try
            {
                GetPrivateProfileString(section, key, "", temp, 500, this.inipath);

                if (!int.TryParse(temp.ToString().Trim(), out itInt))
                {
                    itInt = 0;
                }
            }
            catch
            {
            }
            return itInt;
        }

        /// 
        /// 读bool
        /// 
        /// 
        /// 
        /// 
        public bool IniReadBool(string section, string key)
        {
            bool itInt = false;
            StringBuilder temp = new StringBuilder(500);
            try
            {
                GetPrivateProfileString(section, key, "", temp, 500, this.inipath);

                if (!bool.TryParse(temp.ToString().Trim(), out itInt))
                {
                    itInt = false;
                }
            }
            catch
            {
            }
            return itInt;
        }

        /// 
        /// 读长整数
        /// 
        /// 
        /// 
        /// 
        public long IniReadLong(string section, string key)
        {
            long lLong = 0;
            StringBuilder temp = new StringBuilder(500);
            try
            {
                GetPrivateProfileString(section, key, "", temp, 500, this.inipath);

                if (!long.TryParse(temp.ToString().Trim(), out lLong))
                {
                    lLong = 0;
                }
            }
            catch
            {
            }
            return lLong;
        }

        /// 
        /// 验证文件是否存在
        /// 
        /// 布尔值
        public bool ExistINIFile()
        {
            return File.Exists(inipath);
        }
        /// 
        /// 读double
        /// 
        /// 
        /// 
        /// 
        public double IniReadDouble(string section, string key)
        {
            double lLong = 0;
            StringBuilder temp = new StringBuilder(500);
            try
            {
                GetPrivateProfileString(section, key, "", temp, 500, this.inipath);

                if (!double.TryParse(temp.ToString().Trim(), out lLong))
                {
                    lLong = 0;
                }
            }
            catch
            {
            }
            return lLong;
        }
    }

你可能感兴趣的:(c#)