• 在指定路径下创建文件


    在刚开始做程序员时很多东西都需要从网上查询,试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 { }
            }

  • 相关阅读:
    《快速软件开发》学习笔记 之一
    Python+常用模块(2).md
    Python语法 (1).md
    使用mysql导入txt文件
    Python+numpy(3).md
    笔试二(程序题)
    啦啦啦 我的博客开通了
    java面试笔试
    笔试三(面试二)
    笔试三(面试)
  • 原文地址:https://www.cnblogs.com/mm08290523/p/5280069.html
Copyright © 2020-2023  润新知