文件批量生成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 + ";\n" +
                         "using CoreFramework." + calssName + ";\n" +
                         "using System;\n" +
                         "using System.Collections.Generic;\n" +
                         "using System.Text;\n\n" +

                         "namespace I" + calssName + "\n" +
                            "{\n" +
                                "\tpublic interface I" + x.Name.Substring(0, x.Name.Length - 6) + "" + calssName + " : IBase" + calssName + "<" + x.Name + ">\n" +
                                "\t{\n" +

                                "\t}\n" +
                            "}\n";
                //創建文件夾
                if (!Directory.Exists(@"C:\Users\Desktop\I" + calssName + ""))
                {
                    Directory.CreateDirectory(@"C:\Users\Desktop\I" + calssName + "");
                }
                //創建文件夾
                if (!Directory.Exists(@"C:\Users\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + ""))
                {
                    Directory.CreateDirectory(@"C:\Users\Desktop\I" + calssName + @"\" + x.Name.Substring(0, x.Name.Length - 6) + "");
                }
                //保存 開啟文件流
                using (FileStream fs = new FileStream(@"C:\Users\Desktop\I" + 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不好用,所以就是使用了讀寫了直接寫入吧。

主要就是想記錄一下,好記性不如爛筆頭。所以想博客記錄一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM