• C# 函数2


    //读写INI

        public class GF_INI
        {
            [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);
            [DllImport("kernel32.dll")]
            public static extern int Beep(int dwFreq, int dwDuration);
     
            //读ini
            public static void iniFile_SetVal(string in_filename, string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, in_filename);
            }
     
            //写INI
            public static string iniFile_GetVal(string in_filename, string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, in_filename);
                if (i == 0)
                    return "";
                else
                    return temp.ToString();
            }
        }
     
    //硬件信息
        public class GF_Hardware
        {
            /// <summary>
            /// cpu序列号
            /// </summary>
            /// <returns></returns>
            public static string getID_CpuId()
            {
                string cpuInfo = "";//cpu序列号
                ManagementClass cimobject = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                return cpuInfo;
            }
     
            /// <summary>
            /// 硬盘ID号
            /// </summary>
            /// <returns></returns>
            public static string getID_HardDiskId()
            {
                string HDid = "";
                ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                return HDid;
            }
     
            /// <summary>
            /// 获取网卡MacAddress
            /// </summary>
            /// <returns></returns>
            public static string getID_NetCardId()
            {
                string NCid = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        NCid = mo["MacAddress"].ToString();
                    mo.Dispose();
                }
                return NCid;
            }         
        }
     
    //网络部分
        public class GF_Network
        {
            /*
             * C#完整的通信代码(点对点,点对多,同步,异步,UDP,TCP)   
             * http://topic.csdn.net/u/20080619/08/dcef3fe2-f95b-4918-8edb-36d48a3d0528_2.html
             
             */
     
     
            /// <summary>
            /// 获取IP地址 返回第一个
            /// </summary>
            /// <returns></returns>
            public static string getIP_This()
            {
                IPHostEntry hostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress[] address = hostInfo.AddressList;
                if (address.Length == 0)
                    return "";
                else
                    return address[0].ToString();
            }
     
            /// <summary>
            /// ping IP地址 timeout 局域网用200,广域网用2000
            /// </summary>
            /// <param name="ip">IP地址</param>
            /// <param name="timeout">超时 毫秒</param>
            /// <returns></returns>
            public static bool ping(string ip, int timeout)
            {            
                IPAddress ipadd;
                if (!IPAddress.TryParse(ip, out ipadd))
                {
                    return false;   
                }
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(ip, timeout, new Byte[] { Convert.ToByte(1) });
                if (reply.Status == IPStatus.Success)
                    return true;
                else
                    return false;
            }
            /// <summary>
            /// 判读是否是IP地址
            /// </summary>
            /// <param name="in_str"></param>
            /// <returns></returns>
            public static bool IsIPStr(string in_str)
            {
                if (in_str.Replace(".""").Length != in_str.Length - 3)
                    return false;
                try
                {
                    IPAddress ip = IPAddress.Parse(in_str);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
     
     
    //读写INI
        public class GF_INI
        {
            [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);
            [DllImport("kernel32.dll")]
            public static extern int Beep(int dwFreq, int dwDuration);
     
            //读ini
            public static void iniFile_SetVal(string in_filename, string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, in_filename);
            }
     
            //写INI
            public static string iniFile_GetVal(string in_filename, string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, in_filename);
                if (i == 0)
                    return "";
                else
                    return temp.ToString();
            }
        }
     
     
     //文件操作
        public class GF_File
        {
     
            /// <summary>
            /// 写日志文件
            /// </summary>
            /// <param name="sPath">    年月  例  2011-04</param>
            /// <param name="sFileName">月日  例  04-22</param>
            /// <param name="content">时间+  内容</param>
            /// <returns></returns>
            public static bool WriteLog(string sPath, string sFileName, string content)
            {
                try
                {
     
                    
                    StreamWriter sr;
                    if (!Directory.Exists(sPath))
                    {
                        Directory.CreateDirectory(sPath);
                    }
                    string v_filename = sPath+"\"+ sFileName;
     
     
                    if (!File.Exists(v_filename)) //如果文件存在,则创建File.AppendText对象
                    {
                        sr = File.CreateText(v_filename);
                        sr.Close();
                    }
                    using (FileStream fs = new FileStream(v_filename, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Write))
                    {
                        using (sr = new StreamWriter(fs))
                        {
                             
                            sr.WriteLine(DateTime.Now.ToString("hh:mm:ss")+"     "+ content);
                            sr.Close();
                        }
                        fs.Close();
                    }
                    return true;
     
                }
                catch return false; }
            }
     
     
            /// <summary>
            /// 读取文本文件内容,每行存入arrayList 并返回arrayList对象
            /// </summary>
            /// <param name="sFileName"></param>
            /// <returns>arrayList</returns>
            public static ArrayList ReadFileRow(string sFileName)
            {
                string sLine = "";
                ArrayList alTxt = null;
                try
                {
                    using (StreamReader sr = new StreamReader(sFileName))
                    {
                        alTxt = new ArrayList();
     
                        while (!sr.EndOfStream)
                        {
                            sLine = sr.ReadLine();
                            if (sLine != "")
                            {
                                alTxt.Add(sLine.Trim());
                            }
     
                        }
                        sr.Close();
                    }
                }
                catch
                {
     
                }
                return alTxt;
            }
     
     
            /// <summary>
            /// 备份文件
            /// </summary>
            /// <param name="sourceFileName">源文件名</param>
            /// <param name="destFileName">目标文件名</param>
            /// <param name="overwrite">当目标文件存在时是否覆盖</param>
            /// <returns>操作是否成功</returns>
            public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite)
            {
                if (!System.IO.File.Exists(sourceFileName))
                    throw new FileNotFoundException(sourceFileName + "文件不存在!");
     
                if (!overwrite && System.IO.File.Exists(destFileName))
                    return false;
     
                try
                {
                    System.IO.File.Copy(sourceFileName, destFileName, true);
                    return true;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
     
     
            /// <summary>
            /// 备份文件,当目标文件存在时覆盖
            /// </summary>
            /// <param name="sourceFileName">源文件名</param>
            /// <param name="destFileName">目标文件名</param>
            /// <returns>操作是否成功</returns>
            public static bool BackupFile(string sourceFileName, string destFileName)
            {
                return BackupFile(sourceFileName, destFileName, true);
            }
     
     
            /// <summary>
            /// 恢复文件
            /// </summary>
            /// <param name="backupFileName">备份文件名</param>
            /// <param name="targetFileName">要恢复的文件名</param>
            /// <param name="backupTargetFileName">要恢复文件再次备份的名称,如果为null,则不再备份恢复文件</param>
            /// <returns>操作是否成功</returns>
            public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName)
            {
                try
                {
                    if (!System.IO.File.Exists(backupFileName))
                        throw new FileNotFoundException(backupFileName + "文件不存在!");
     
                    if (backupTargetFileName != null)
                    {
                        if (!System.IO.File.Exists(targetFileName))
                            throw new FileNotFoundException(targetFileName + "文件不存在!无法备份此文件!");
                        else
                            System.IO.File.Copy(targetFileName, backupTargetFileName, true);
                    }
                    System.IO.File.Delete(targetFileName);
                    System.IO.File.Copy(backupFileName, targetFileName);
                }
                catch (Exception e)
                {
                    throw e;
                }
                return true;
            }
     
            public static bool RestoreFile(string backupFileName, string targetFileName)
            {
                return RestoreFile(backupFileName, targetFileName, null);
            }
        }
     
     
  • 相关阅读:
    Linux下编译安装redis
    docker搭建swoole的简易的聊天室
    Linux下面安装swoole
    laravel命令
    史上最污技术解读,60 个 IT 术语我竟然秒懂了.....
    redis集群搭建
    Windows安装MongoDB
    十大经典排序算法(动图演示)
    消息中间件基础
    laravel邮件发送
  • 原文地址:https://www.cnblogs.com/liufei88866/p/3478352.html
Copyright © 2020-2023  润新知