zoukankan      html  css  js  c++  java
  • 指定路径创建文件,并写入数据(加载配置文件)

    引用:https://www.cnblogs.com/likui-bookHouse/p/11951128.html

    很多时候,我们需要把重要信息输出到某个文件中,进行查阅。

    思路:创建文件夹(有则跳过)==> 创建文件(是否覆盖,是否删除之前的文件,是否保留某一个时间段的文件)==>写入

    示例:向当前项目路径添加文件夹,并向里面创建文件(仅保留当天的文件),今天之前的文件连同文件夹一起删除,然后重新创建今天的文件及文件夹

    代码:

    复制代码
     /// <summary>
            /// 写入文件
            /// </summary>
            /// <param name="infos"></param>
            public void WriteToFile(List<OutputInfo> infos)
            {
    
    
                try
                {
                    #region 
    
                    var baseDir = AppDomain.CurrentDomain.BaseDirectory;
                    var path = baseDir + @_dicConfig["OutPutPath"].ToString();
                    //删除不是当天的文件夹和文件
                    DelDirectory(path);
                    //判断是否有这个文件夹
                    var filePath = $"{path}/IPURL_{DateTime.Now.ToString("yyyyMMdd")}";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    var pathf = $"{filePath}/{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
                    #endregion
    
    
                    FileStream fs = new FileStream(pathf, FileMode.Create, FileAccess.Write);
                    //System.IO.File.SetAttributes(path, FileAttributes.Hidden);
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入值
                    sw.WriteLine("程序开始写入IP-URL信息...");
                    foreach (var item in infos)
                    {
                        sw.WriteLine("Ip:" + item.IP + ",Url:" + item.URL);
                    }
                    sw.WriteLine("程序写入完成");
                    sw.Close();
                    fs.Close();
    
                }
                catch (Exception ex)
                {
                    Log.Write("error[WriteToFile]:" + ex.Message, SWLog.LogLevel.D错误事件);
                }
            }
    
            /// <summary>
            /// 删除文件和文件夹(保留当天)
            /// </summary>
            /// <param name="folderPath"></param>
            public void DelDirectory(string folderPath)
            {
                if (System.IO.File.Exists(folderPath))
                {
                    DirectoryInfo dyInfo = new DirectoryInfo(folderPath);
                    //获取文件夹下所有的文件夹
                    foreach (DirectoryInfo feInfo in dyInfo.GetDirectories())
                    {
                        //判断文件日期是否小于今天,是则删除
                        if (feInfo.CreationTime < DateTime.Today)
                            feInfo.Delete(true);
                    }
                    //获取文件夹下所有的文件
                    foreach (FileInfo feInfo in dyInfo.GetFiles())
                    {
                        feInfo.Delete();
                    }
                }
    
            }
    复制代码

    加载配置文件:

    复制代码
       /// <summary>
            /// 获取配置信息
            /// </summary>
            /// <param name="pluginName">插件名称</param>
            /// <param name="isTrim">插件名称</param>
            /// <returns></returns>
            protected Dictionary<string, string> GetDicConfig(string pluginName, bool isTrim = true)
            {
                var retdic = new Dictionary<string, string>();
                // config配置文件全路径
                var localConfigPath = string.Format("{0}/plugins/{1}/config.txt", AppDomain.CurrentDomain.BaseDirectory, pluginName);
                if (File.Exists(localConfigPath) != false)
                {
                    using (System.IO.StreamReader sr = new StreamReader(localConfigPath))
                    {
                        string tmp = string.Empty;
                        while ((tmp = sr.ReadLine()) != null)
                        {
                            if (tmp.TrimStart().StartsWith("#"))
                            {
                                continue;
                            }
    
                            string tmp_ary;
                            if (isTrim)
                            {
                                tmp_ary = tmp.Trim().Split('#')[0].Trim();
                            }
                            else
                            {
                                tmp_ary = tmp.Split('#')[0];
                            }
    
                            var ary = tmp_ary.Split('=');
                            if (ary.Length >= 2)
                            {
                                retdic.Add(ary[0].Trim(), tmp_ary.Substring(ary[0].Length + 1, tmp_ary.Length - ary[0].Length - 1));
                            }
                        }
                    }
                    Log.Write(string.Format("开始加载配置文件:{0}.", localConfigPath), SWLog.LogLevel.G有用信息);
                }
                else
                {
                    Log.Write(string.Format("未发现配置文件:{0}.", localConfigPath), SWLog.LogLevel.G有用信息);
                }
                return retdic;
            }
    复制代码

    配置文件路径:

    获取的配置文件信息字典:

    写入日志如下:

    HK
  • 相关阅读:
    图片大于父元素时的居中
    nth-of-type和nth-child
    移动端图片轮播—swipe滑动插件
    延迟加载图片并监听图片加载完成
    css sprites 多张图片整合在一张图片上
    不同tab下的列表长度不同,tab的样式和底部的位置不同
    移动端多行文本溢出省略
    在64位windows 7上安装汇编调试工具debug.exe的方法
    批量删除亚马逊kindle云端文档
    使用key链接远程Git仓库
  • 原文地址:https://www.cnblogs.com/HarryK4952/p/14295353.html
Copyright © 2011-2022 走看看