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 { }
            }

  • 相关阅读:
    (OK) port_lighttpd_to_Android——没有基于android 4.4源码
    Linux添加头文件路径—INCLUDE_PATH
    (OK) 交叉编译hello.c for android (--sysroot),不使用Android.mk和ndk-build
    Building and running Node.js for Android
    编译node-v4.2.1,出现错误:undefined reference to getpwuid_r
    我为什么向后端工程师推荐Node.js
    (OK) 编译 pcre-8.37 静态库
    port_lighttpd_to_Android——基于android 4.4源码
    深受C/C++程序员欢迎的11款IDE
    推荐!国外程序员整理的 PHP 资源大全
  • 原文地址:https://www.cnblogs.com/mm08290523/p/5280069.html
Copyright © 2011-2022 走看看