.net中如何把多個檔案合併成一個檔案
阿新 • • 發佈:2019-01-02
懶得再單獨寫一個,所以就加了超級詳細的註釋
前幾個判斷主要是合併sql檔案,什麼樣的sql對應的什麼樣的處理,
最後一個判斷是合併任何檔案
方法的兩個引數:第一個是開啟檔案路徑的集合,第二個引數檔案儲存路徑
private void FileMerge(string[] filePath,string outFilePath) { try { int b; int n = filePath.Length; //合併配置檔案 //FileStream[] fileIn = new FileStream[n]; using (FileStream fileOut = new FileStream(outFilePath, FileMode.CreateNew)) //建立檔案 { string[] namezu = f1.hanshuName(); //宣告一個存放儲存過程名稱的陣列並賦值 foreach (string func in namezu) //迴圈陣列 { if (func != null & func != "") { string funcName = func; //下面是拼接介面的配置資訊 //MessageBox.Show(funcName); string strSql1 = "exec proc_insert 'SYS_DEF_ASI_FUNC' ,'funcname=''" + funcName + "'' and COALESCE(inuse,0)=1 '"; DataSet ds1 = DM.ReturnTableEx(strSql1); string txt1 = ""; string txt2 = ""; txt1 = ds1.Tables[0].Rows[0]["column1"].ToString(); string strSql2 = "exec proc_insert 'SYS_DEF_ASI_Func_PARAM' ,'funcname=''" + funcName + "'' and COALESCE(inuse,0)=1 '"; DataSet ds2 = DM.ReturnTableEx(strSql2); foreach (DataRow dr2 in ds2.Tables[0].Rows) { string txt3 = ""; txt3 = dr2["column1"].ToString(); if (!string.IsNullOrEmpty(txt3)) { txt2 = txt2 + "\r\n" + txt3; } } string txt = ""; txt += "-----------------------------------" + funcName + "------------------------------\r\n"; if (!string.IsNullOrEmpty(txt1)) { txt = txt + "\r\n" + " UPDATE SYS_DEF_ASI_FUNC SET INUSE=0 WHERE FUNCNAME='" + funcName + "' " + "\r\n" + txt1 + "\r\n"; } if (!string.IsNullOrEmpty(txt2)) { txt = txt + "\r\n" + " UPDATE SYS_DEF_ASI_Func_PARAM SET INUSE=0 WHERE FUNCNAME='" + funcName + "' " + "\r\n" + txt2 + "\r\n"; } //拼接配置資訊結束 StreamWriter sw = new StreamWriter(fileOut,Encoding.UTF8); sw.Write(txt);//配置資訊寫入寫入文字 sw.Flush(); } } //合併檔案 for (int i = 0; i < n; i++) { if (filePath[i] == null) { break; } try { //分割 StreamReader st = new StreamReader(filePath[i], Encoding.Default); //獲取檔案 StreamWriter sw = new StreamWriter(fileOut, Encoding.UTF8); //宣告寫入 string name = st.ReadToEnd(); //讀取檔案 int CreateProc = name.IndexOf("PROCEDURE"); int AlterProc = name.IndexOf("PROCEDURE"); //判斷是否是儲存過程 if (CreateProc!=-1 || AlterProc!=-1){ name = name.Replace("ALTER", "CREATE"); //文字中存在ALTER那麼就替換成CREATE int a = name.IndexOf("-- Author"); //先找-- author if (a == -1) //如果沒有找到author { a = name.IndexOf("ALTER"); } if (a == -1) { a = name.IndexOf("CREATE"); } string file = name.Substring(a); // 作者之前的分割掉 擷取作者之後的全部內容 int length = file.Length; string GOFile = file.Substring(length-20,20); int Goindex = GOFile.IndexOf("GO"); //判斷文字最後有沒有GO if(Goindex==-1){ file += "\r\nGO\r\n"; //如果沒有再文字的最後新增GO } int endindex = file.IndexOf("@"); //獲取分割之後的文字中第一次出現@的下表:為分割儲存過程名稱的結束下表 int strtindex = file.IndexOf("PROCEDURE"); //獲取分割之後的文字中第一次出現PROCEDURE的下表:為分割儲存過程名稱的開始下表 string filename = file.Substring(strtindex + 9, ((endindex - strtindex) -9)).Trim();//擷取儲存過程名稱 int endindex1 = filename.IndexOf("\r\n"); //獲取分割之後的文字中第一行 if (endindex1!=-1) //判斷儲存過程名稱是否有好幾行 { filename = filename.Substring(0, endindex1).Trim(); } filename=filename.Replace("[dbo].",""); //替換儲存過程名稱中的[dbo]. filename = filename.Replace("[", ""); //替換文字中的[ filename = filename.Replace("]", ""); //替換文字中的] string txt = "\r\n-----------------------------" + filename + "儲存過程-------------------------------------\r\n";//拼接一個儲存過程的if判斷,存在這個儲存過程則刪除 txt += "\r\nIF EXISTS(SELECT * FROM sysobjects WHERE type='p' AND NAME='" + filename + "')\r\nBEGIN\r\n DROP PROC " + filename + "\nEND\ngo"; file = txt + "\r\n" + "-- =============================================\r\n" + file;//儲存過程的判斷和分割後的文字進行拼接 sw.Write(file); //寫入文字 sw.Flush(); } int CreateView = name.IndexOf("VIEW"); int AlterView = name.IndexOf("VIEW"); //判斷是否是檢視(view) if (CreateView != -1 || AlterView != -1) { int fileLength=name.Length; //獲取文字的長度 string File = name; File = File.Replace("ALTER", "CREATE"); //替換ALTER為CREATE int ViewIndex = File.IndexOf("CREATE"); File = File.Substring(ViewIndex, ((fileLength - ViewIndex))).Trim(); //獲取檢視內容 int length = File.Length; string GOFile = File.Substring(length - 20, 20); int Goindex = GOFile.IndexOf("GO"); //判斷文字最後有沒有GO if (Goindex == -1) { File += "\r\nGO\r\n"; //如果沒有再文字的最後新增GO } int FileASLength = File.IndexOf("\n"); //換行的位置 int staIndex = File.IndexOf("CREATE"); string FileName = File.Substring(staIndex + 11, ((FileASLength - staIndex)-11)).Trim();//擷取檢視名稱 FileName = FileName.Replace("[dbo].", ""); //替換檢視名稱中的[dbo]. FileName = FileName.Replace("[", ""); //替換文字中的[ FileName = FileName.Replace("]", ""); //替換文字中的] string txt = "\r\n-----------------------------" + FileName + "檢視-------------------------------------\r\n";//拼接一個檢視的if判斷,存在這個儲存過程則刪除 txt += "\r\nIF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'" + FileName + "'))\r\n DROP VIEW " + FileName + "\r\nGO\r\n"; File = txt + "\r\n" + File;//檢視的判斷和分割後的文字進行拼接 sw.Write(File); //寫入文字 sw.Flush(); } //判斷函式 int CreateFunction = name.IndexOf("FUNCTION"); int AlterFunction = name.IndexOf("FUNCTION"); if (CreateFunction != -1 || AlterFunction!=-1) { int fileLength = name.Length; //獲取文字的長度 string File = name; File = File.Replace("ALTER", "CREATE"); //替換ALTER為CREATE int FUNCTIONIndex = File.IndexOf("CREATE"); File = File.Substring(FUNCTIONIndex, ((fileLength - FUNCTIONIndex))).Trim(); //獲取函式內容 int length = File.Length; string GOFile = File.Substring(length - 20, 20); int Goindex = GOFile.IndexOf("GO"); //判斷文字最後有沒有GO if (Goindex == -1) { File += "\r\nGO\r\n"; //如果沒有再文字的最後新增GO } int FileASLength = File.IndexOf("\n"); //換行的位置 int staIndex = File.IndexOf("CREATE"); string FileName = File.Substring(staIndex + 16, ((FileASLength - staIndex) - 16)).Trim();//擷取函式名稱 int FileZGH = FileName.IndexOf("("); if (FileZGH != -1) //判斷函式名稱是否有好幾行 { FileName = FileName.Substring(0, FileZGH).Trim(); } if (FileZGH != -1) //判斷函式名稱是否有好幾行 { FileName = FileName.Substring(0, FileZGH).Trim(); } FileName = FileName.Replace("[dbo].", ""); //替換函式名稱中的[dbo]. FileName = FileName.Replace("[", ""); //替換文字中的[ FileName = FileName.Replace("]", ""); //替換文字中的] string txt = "\r\n-----------------------------" + FileName + "函式-------------------------------------\r\n";//拼接一個函式的if判斷,存在這個函式則刪除 txt += "\nIF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + FileName + "') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))\r\n DROP FUNCTION " + FileName + "\r\nGO\r\n"; File = txt + "\r\n" + File;//函式的判斷和分割後的文字進行拼接 sw.Write(File); //寫入文字 sw.Flush(); } if (CreateProc == -1 && CreateFunction == -1 && CreateView==-1) { string File = name; string GOFile = File.Substring(File.Length - 20, 20); int Goindex = GOFile.IndexOf("GO"); //判斷文字最後有沒有GO if (Goindex == -1) { File += "\r\nGO\r\n"; //如果沒有再文字的最後新增GO } int nameIndex = filePath[i].LastIndexOf(@"\"); string FileName = filePath[i].Substring(nameIndex+1,filePath[i].Length-nameIndex-1); string txt = "\r\n--------------------------------------" + FileName + "--------------------------------------------\r\n";//拼接一個函式的if判斷,存在這個函式則刪除 File = txt + "\r\n" + File;//拼接 sw.Write(File); //寫入文字 sw.Flush(); } if (filePath.Length - 1 == i) { sw.Close(); //最後一次寫完關閉 fileOut.Close(); } //while ((b = fileIn[i].ReadByte()) != -1) // fileOut.WriteByte((byte)b); } catch (System.Exception ex) { Console.WriteLine(ex.Message); } //finally //{ // fileIn[i].Close(); //} } } FN.MsgBox("提示資訊", "生成成功。", 0 + 48); } catch (Exception ex) { //異常 return; } }