zoukankan      html  css  js  c++  java
  • 在指定路径下创建文件

    在刚开始做程序员时很多东西都需要从网上查询,试N多种才可以找到符合自己的。

    现在这个时间我想分享的就是创建文件。

            /// <summary>
            /// 写日常日志,每月一个日常日志文件
            /// </summary>
            /// <param name="filename">自定义文件名字</param>
            /// <param name="message">日志内容</param>
            public static void WriteCommonLog(string filename, string message)
            {
                try
                {
                    if (message != "" && filename != "")
                    {
                        string filepath = System.Web.HttpContext.Current.Server.MapPath("~/framework/Log/");//Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径
                        if (!System.IO.Directory.Exists(filepath))
                        {
                            System.IO.Directory.CreateDirectory(filepath);
                        }
                        filepath += "log_" + DateTime.Now.Year + "." + DateTime.Now.Month + "." + filename + ".txt";
                        if (!System.IO.File.Exists(filepath))
                        {
                            using (System.IO.FileStream filestream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
                            {
                                filestream.Close();
                            }
                        }
                        try
                        {
                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filepath, true))
                            {
                                sw.WriteLine(DateTime.Now);
                                sw.WriteLine(message);
                                sw.WriteLine("");
                                sw.Close();
                            }
                        }
                        catch { }
                    }
                }
                catch { }
            }

  • 相关阅读:
    adodb.stream文件操作类详解
    Html中Label标记的作用和使用介绍
    正则表达式的威力轻松消除HTML代码
    只需一行代码就能让IE 6崩溃
    码农干货系列【17】Wind.js与Promise.js
    码农干货系列【3】割绳子(cut the rope)制作点滴:旋转(rotation)
    HTML5 Canvas开发者和读者的福音
    码农干货系列【8】世界上最简单的3D渲染(no webgl)
    码农干货系列【18】getting started with Promise.js(总)
    ProgressForm
  • 原文地址:https://www.cnblogs.com/mm08290523/p/5280069.html
Copyright © 2011-2022 走看看