写入文本到TXT

        /// <summary>
        /// 写入文本到TXT(如果有文件不进行操作)
        /// </summary>
        /// <param name="strPath">TXT储存路径</param>
        /// <param name="strTXT">文本内容</param>
        /// <returns>成功返回0,失败返回-1</returns>
        public static int WriteFile(string strPath, string strTXT)
        {
            try
            {
                string strFolderPath = Path.GetDirectoryName(strPath);
                if (!Directory.Exists(strFolderPath))
                {
                    Directory.CreateDirectory(strFolderPath);
                }
                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;
        }
        /// <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);
                }
                if (boolCover)
                {
                    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;
        }
        /// <summary>
        /// 写入文本到TXT,追加写入文件(如果不存在自动创建)
        /// </summary>
        /// <param name="strPath">TXT储存路径</param>
        /// <param name="strTXT">文本内容</param>
        /// <returns>成功返回0,失败返回-1</returns>
        public static int AppendFile(string strPath, string strTXT)
        {
            try
            {
                string strFolderPath = Path.GetDirectoryName(strPath);
                if (!Directory.Exists(strFolderPath))
                {
                    Directory.CreateDirectory(strFolderPath);
                }
                FileStream filestream = new FileStream(strPath, FileMode.Append, 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;
        }
        /// <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);
                }
                FileStream filestream = new FileStream(strPath, FileMode.Append, FileAccess.Write);
                StreamWriter streamwriter = new StreamWriter(filestream);
                if (boolWrap)
                {
                    streamwriter.Write("\r\n");
                }
                streamwriter.Write(strTXT);
                streamwriter.Close();
                filestream.Close();
            }
            catch (Exception ex)
            {
                TXTHelper.Logs(ex.ToString());
                return -1;
            }
            return 0;
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值