• 写文件 C#


    常用方法:

    public class FileHelper
    {
        private static void Write(string fileName, byte[] bytes)
        {
            FileStream fs = null;
            string filePath = "C:\\temp\\" + fileName + ".ecg";
            Encoding encoder = Encoding.UTF8;
            try
            {
    
                //判断文件是否存在
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                fs = File.OpenWrite(filePath);
                fs.Position = fs.Length;
                fs.Write(bytes, 0, bytes.Length);
            }
            catch (Exception ex)
            {
                NLogHelper.Error(ex.Message);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }
    
        public static void Write(string msg)
        {
            System.IO.FileStream fs = null;
            System.IO.StreamWriter sw = null;
            try
            {
                string time = System.DateTime.Now.ToString();
                var fileName = "C:\\temp\\vipsoft.log";
                string filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
                fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
                sw = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
                sw.WriteLine("======================");
                sw.WriteLine(time);
                sw.WriteLine("----------------------");
                sw.WriteLine(msg);
                sw.WriteLine("----------------------");
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (sw != null)
                {
                    sw.Close();
                }
            }
        }
    
        /// <summary>
        /// 用得比较多
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteAppend(string msg)
        {
            System.IO.StreamWriter sw = null;
            try
            {
                var fileName = "C:\\temp\\vipsoft.log";
                //判断文件是否存在
                if (!Directory.Exists(fileName))
                {
                    Directory.CreateDirectory(fileName);
                }
                sw = File.AppendText(fileName); 
                sw.WriteLine(msg);
                sw.Flush();
                sw.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }
    
        }
    
    }
  • 相关阅读:
    题解「CF204E Little Elephant and Strings」
    题解「CF1000G Two-Paths」
    消息机制及按钮实效性
    View(视图)——消息机制
    城市线程练习题后续
    城市线程练习题
    View(视图)——对话框之日期对话框和时间对话框文集
    View(视图)——对话框之进度对话框
    删除对话框练习
    拨打电话与发送短信功能
  • 原文地址:https://www.cnblogs.com/vipsoft/p/16473746.html
Copyright © 2020-2023  润新知