多个js文件合并为一个大js文件的方法

来源:互联网 发布:手机壁纸软件may 编辑:程序博客网 时间:2024/05/20 04:29
 public void mergeJS(string sourcedir,string mergeAfterPathFileName)        {            string dirPath = this.Server.MapPath(sourcedir); //"~/Scripts/cform/";            if (Directory.Exists(dirPath))            {                //获得目录信息                DirectoryInfo dir = new DirectoryInfo(dirPath);                //获得目录文件列表                FileInfo[] files = dir.GetFiles("*.*");                if (files != null && files.Length > 0)                {                    string path = this.Server.MapPath(mergeAfterPathFileName); //"~/Scripts/formloader.js";                    StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);   //追加方式                    //                    string fname = "";                                       foreach (FileInfo fileInfo in files)                    {                        fname = fileInfo.Name;                        string js_path=dirPath+fname;                        StreamReader sr=new StreamReader(js_path);                                                while(sr.EndOfStream==false)                        {                            sw.WriteLine(sr.ReadLine());                        }                        sr.Close();                        sr.Dispose();                        sr = null;                    }                    sw.Flush();                    sw.Close();                    sw.Dispose();                                    }                else                {                    this.Response.Write("目录下没获取到文件,没执行生成操作!");                }            }            else            {                this.Response.Write("目录不存在!请重新设置目录");            }        }
public void CreateJS_formloader_js()        {            string mergeAfterPName = "~/Scripts/formloader.js";            string path = this.Server.MapPath(mergeAfterPName);            if (System.IO.File.Exists(path) == true)            {                System.IO.File.Delete(path);            }            mergeJS("~/Scripts/sys/", mergeAfterPName);                        mergeJS("~/Scripts/cform/", mergeAfterPName);            mergeJS("~/Scripts/chart/", mergeAfterPName);            this.Response.Write("<br/>");            this.Response.Write(mergeAfterPName+"生成js完成!");        }


0 0