码迷,mamicode.com
首页 > 其他好文 > 详细

创建文件

时间:2016-11-17 10:48:58      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:style   app   路径   path   ram   显示   writer   void   创建文件夹   

1、创建txt文本

     /// <summary>
        /// log日志,txt的
        /// </summary>
        /// <param name="Log1">内容</param>
        /// <param name="name">名字</param>
        /// <param name="path">路径</param>
        public void Logmsg(string Log1, string name, string path)
        {
            #region 创建日志
            Log = "";
            Log += Log1 + "\r\n";

            //生成目录
            //创建文件夹
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);
            }

            // 判断文件是否存在,不存在则创建,否则读取值显示到txt文档
            if (!System.IO.File.Exists(path + "/" + name + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt"))
            {
                FileStream fs1 = new FileStream(path + "/" + name + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件 
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(Log);//开始写入值
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(path + "/" + name + "_Log" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt" + "", FileMode.Append, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(Log);//开始写入值
                sr.Close();
                fs.Close();
            }
            #endregion
        }

 

创建文件

标签:style   app   路径   path   ram   显示   writer   void   创建文件夹   

原文地址:http://www.cnblogs.com/cang12138/p/6072541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!