zoukankan      html  css  js  c++  java
  • 文件批量生成IO流读写

    /// <summary>
            /// 生成文件的
            /// </summary>
            /// <param name="calssName"></param>
            public void create(string calssName)
            {
                //获取程序集
                var createClass = Assembly.Load("Entity");
                //反射出所有的类
                List<Type> ts = createClass.GetTypes().ToList();
                //循环生成
                ts.ForEach(x =>
                {
                    //x.Namespace获取命名空间
                    var ss = "using " + x.Namespace + ";
    " +
                             "using CoreFramework." + calssName + ";
    " +
                             "using System;
    " +
                             "using System.Collections.Generic;
    " +
                             "using System.Text;
    
    " +
    
                             "namespace I" + calssName + "
    " +
                                "{
    " +
                                    "	public interface I" + x.Name.Substring(0, x.Name.Length - 6) + "" + calssName + " : IBase" + calssName + "<" + x.Name + ">
    " +
                                    "	{
    " +
    
                                    "	}
    " +
                                "}
    ";
                    //创建文件夹
                    if (!Directory.Exists(@"C:UsersDesktopI" + calssName + ""))
                    {
                        Directory.CreateDirectory(@"C:UsersDesktopI" + calssName + "");
                    }
                    //创建文件夹
                    if (!Directory.Exists(@"C:UsersDesktopI" + calssName + @"" + x.Name.Substring(0, x.Name.Length - 6) + ""))
                    {
                        Directory.CreateDirectory(@"C:UsersDesktopI" + calssName + @"" + x.Name.Substring(0, x.Name.Length - 6) + "");
                    }
                    //保存 开启文件流
                    using (FileStream fs = new FileStream(@"C:UsersDesktopI" + calssName + @"" + x.Name.Substring(0, x.Name.Length - 6) + @"I" + x.Name.Substring(0, x.Name.Length - 6) + "" + calssName + ".cs", FileMode.Create))
                    {
                        //文本写入 开启读写流
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.Write(ss);
                        }
                    }
                });
            }

    最近在项目中使用了仓储模式有些代码太过于重复了所以想写个生成。

    本来是想用T4模板的,但是感觉T4不好用,所以就是使用了读写了直接写入吧。

    主要就是想记录一下,好记性不如烂笔头。所以想博客记录一下。

  • 相关阅读:
    hnoi2013
    图片屏幕LibGdxGearJoint齿轮关节
    数据属性WEKA学习总结
    函数调用[置顶] C/C++在main函数之前和之后会做些什么
    系统控制2013北京照明展后记
    终端安装解决svn "cannot set LC_CTYPE locale"的问题
    系统服务器Fedora和Red Hat Enterprise Linux实用指南(第6版)(上、下册)( 入行必读的Linux圣经)
    文件数据库Android面试题(三)
    UML中的活动图
    UML模型
  • 原文地址:https://www.cnblogs.com/chenxi001/p/11731066.html
Copyright © 2011-2022 走看看