• 读写txt文件类


     public class TxtFileManage
        {
            public static void Save(string txt)
            {
                try
                {
                    FileStream fileStream = new FileStream(CommonStatic.EmailTxtFilePath, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite);
                    StreamWriter sw = new StreamWriter(fileStream);
                    sw.WriteLine(ReadFile() + "\n" + txt);
                    sw.Close();
                    fileStream.Close();
                }
                catch (Exception ex)
                {
                    LogWriter.LogErrorEvent("Nagios", ex.Message);
                }
            }
            private static string ReadFile()
            {
                StringBuilder sb =new StringBuilder();
                if (File.Exists(CommonStatic.EmailTxtFilePath))
                {
                    try
                    {
                        FileStream fs = new FileStream(CommonStatic.EmailTxtFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        StreamReader sr = new StreamReader(fs);
                        String lineStr;
                        while ((lineStr = sr.ReadLine()) != null)
                        {
                            sb.AppendLine(lineStr);
                        }
                        sr.Close();
                        fs.Close();
                    }
                    catch (Exception ex)
                    {
                        LogWriter.LogErrorEvent("Nagios", ex.Message);
                    }
                }
                return sb.ToString();
            }
            public static Boolean IsContainTxt(string txt)
            {
                bool result = false;
                if(File.Exists(CommonStatic.EmailTxtFilePath))
                {
                    try
                    {
                        string[] contents = File.ReadAllLines(CommonStatic.EmailTxtFilePath, Encoding.Default);
                        foreach (string str in contents)
                        {
                            if (str.Trim() == txt.Trim())
                            {
                                result = true;
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogWriter.LogErrorEvent("Nagios", ex.Message);
                    }
                }
                return result;
            }
        }

  • 相关阅读:
    input框限制0开头的数字(0除外)
    圆角头像----CSS3特效
    html中div获取焦点,去掉input div等获取焦点时候的边框
    一些常用的html css整理--文本长度截取
    html5本地存储
    div块级元素获取焦点
    Intellij IDEA 搜索文件内容
    web安全漏洞防护
    Intellij IDEA 自动生成 serialVersionUID
    mysql 年龄计算(根据生日)
  • 原文地址:https://www.cnblogs.com/zhangsongshan/p/3056157.html
Copyright © 2020-2023  润新知