Пример #1
0
 /// <summary>
 /// 写入文本到TXT(选择是否覆盖)
 /// </summary>
 /// <param name="strPath">TXT储存路径</param>
 /// <param name="strTXT">文本内容</param>
 /// <param name="boolCover">(true)如果冲突覆盖文件,(false)如果冲突不进行操作</param>
 /// <returns>成功返回0,失败返回-1</returns>
 public static int WriteFile(string strPath, string strTXT, bool boolCover)
 {
     try
     {
         //如果路径下的文件不存在,自动创建
         string strFolderPath = Path.GetDirectoryName(strPath);
         if (!Directory.Exists(strFolderPath))
         {
             Directory.CreateDirectory(strFolderPath);
         }
         //写入文本到TXT(选择是否覆盖)
         if (boolCover == true)
         {
             FileStream   filestream   = new FileStream(strPath, FileMode.Create, FileAccess.Write);
             StreamWriter streamwriter = new StreamWriter(filestream);
             streamwriter.Write(strTXT);
             streamwriter.Close();
             filestream.Close();
         }
         else
         {
             FileStream   filestream   = new FileStream(strPath, FileMode.CreateNew, FileAccess.Write);
             StreamWriter streamwriter = new StreamWriter(filestream);
             streamwriter.Write(strTXT);
             streamwriter.Close();
             filestream.Close();
         }
     }
     catch (Exception ex)
     {
         TXTHelper.Logs(ex.ToString());
         return(-1);
     }
     return(0);
 }
Пример #2
0
 /// <summary>
 /// 写入文本到TXT,追加写入文件(如果不存在自动创建)(是否换行)
 /// </summary>
 /// <param name="strPath">TXT储存路径</param>
 /// <param name="strTXT">文本内容</param>
 /// <param name="boolWrap">(true)换行,(false)不换行</param>
 /// <returns>成功返回0,失败返回-1</returns>
 public static int AppendFile(string strPath, string strTXT, bool boolWrap)
 {
     try
     {
         //如果路径下的文件不存在,自动创建
         string strFolderPath = Path.GetDirectoryName(strPath);
         if (!Directory.Exists(strFolderPath))
         {
             Directory.CreateDirectory(strFolderPath);
         }
         //写入文本到TXT,追加写入文件(如果不存在自动创建)(是否换行)
         FileStream   filestream   = new FileStream(strPath, FileMode.Append, FileAccess.Write);
         StreamWriter streamwriter = new StreamWriter(filestream);
         if (boolWrap == true)
         {
             streamwriter.Write("\r\n");
         }
         streamwriter.Write(strTXT);
         streamwriter.Close();
         filestream.Close();
     }
     catch (Exception ex)
     {
         TXTHelper.Logs(ex.ToString());
         return(-1);
     }
     return(0);
 }
Пример #3
0
 /// <summary>
 /// 读取TXT文件中的文本(按照每行存到string[]中)
 /// </summary>
 /// <param name="strPath">TXT文件路径</param>
 /// <returns>TXT文件中的文本(string[])</returns>
 public static string[] GetFileArray(string strPath)
 {
     string[] strText = null;
     try
     {
         strText = File.ReadAllLines(strPath);
     }
     catch (Exception ex)
     {
         TXTHelper.Logs(ex.ToString());
     }
     return(strText);
 }
Пример #4
0
        /// <summary>
        /// 读取TXT文件中的文本
        /// </summary>
        /// <param name="strPath">TXT文件路径</param>
        /// <returns>TXT文件中的文本</returns>
        public static string GetFileString(string strPath)
        {
            string strText = "";

            try
            {
                strText = File.ReadAllText(strPath);
            }
            catch (Exception ex)
            {
                TXTHelper.Logs(ex.ToString());
            }
            return(strText);
        }
Пример #5
0
        /// <summary>
        /// 读取TXT文件中的文本(按照每行存到listString中)
        /// </summary>
        /// <param name="strPath">TXT文件路径</param>
        /// <returns>TXT文件中的文本(listString)</returns>
        public static List <string> GetFileList(string strPath)
        {
            string[]      strText  = null;
            List <string> listText = new List <string>();

            try
            {
                strText = File.ReadAllLines(strPath);
                foreach (string strLine in strText)
                {
                    listText.Add(strLine);
                }
            }
            catch (Exception ex)
            {
                TXTHelper.Logs(ex.ToString());
            }
            return(listText);
        }