@@ -19,25 +19,76 @@ public Form1()
1919 {
2020 InitializeComponent ( ) ;
2121 }
22+
23+ #region 屬性
24+ //路徑設定
25+ public static string FilePath = $@ "{ Directory . GetCurrentDirectory ( ) } { "\\ " } "; //當前工作路徑
26+ public static string LogPath = $@ "{ FilePath } { "\\ " } { "log" } "; //Log檔放置路徑
27+
28+
29+ //資料庫連線區啟用設定
2230 public void ConnectionSettings ( bool Status )
2331 {
2432 DBLocation . Enabled = Status ;
2533 DBName . Enabled = Status ;
2634 UserName . Enabled = Status ;
2735 password . Enabled = Status ;
2836 }
37+ //資料夾路徑&顯示項目區域啟用設定
2938 public void FolderPathCheckedListSettings ( bool Status )
3039 {
3140 FolderPath . Enabled = Status ;
3241 SelectFolder . Enabled = Status ;
3342 FileCheckedList . Enabled = Status ;
3443 }
44+ //匯出list.sql&執行scripts區域啟用設定
3545 public void ScriptBtnSettings ( bool Status )
3646 {
3747 OutputScripts . Enabled = Status ;
3848 RunScript . Enabled = Status ;
3949 }
40- private void SelectFolder_Click ( object sender , EventArgs e )
50+ #endregion
51+
52+ #region 讀取\寫入
53+ /// <summary>
54+ /// 清空檔案內容
55+ /// </summary>
56+ /// <param name="path"></param>
57+ public void ContentClear ( string path )
58+ {
59+ FileStream fs ;
60+ fs = new FileStream ( path , FileMode . Truncate , FileAccess . Write ) ; //Truncate模式打開文件可以清空。
61+ fs . Close ( ) ;
62+ }
63+ /// <summary>
64+ /// 寫入檔案內容(ANSI編碼)
65+ /// </summary>
66+ /// <param name="path"></param>
67+ /// <param name="content"></param>
68+ public void ContentWrite ( string path , string content )
69+ {
70+ using ( StreamWriter writetext = new StreamWriter ( path , true , Encoding . Default ) )
71+ {
72+ writetext . WriteLine ( content ) ;
73+ }
74+ }
75+ /// <summary>
76+ /// 讀取檔案內容
77+ /// </summary>
78+ /// <param name="path"></param>
79+ /// <returns readtext></returns>
80+ public string ContentRead ( string path )
81+ {
82+ string readtext = "" ;
83+ using ( StreamReader reader = new StreamReader ( path , Encoding . Default ) )
84+ {
85+ readtext = reader . ReadToEnd ( ) ;
86+ }
87+ return readtext ;
88+ }
89+ #endregion
90+
91+ private void SelectFolder_Click ( object sender , EventArgs e )
4192 {
4293 FolderPath . Clear ( ) ;
4394 FileCheckedList . Items . Clear ( ) ;
@@ -60,8 +111,6 @@ private void SelectFolder_Click(object sender, EventArgs e)
60111 // 讀取資料夾中檔案名稱
61112 foreach ( string fname in System . IO . Directory . GetFiles ( filepath ) )
62113 {
63- filename = "" ;
64- //filename = filename + fname + "\r\n";
65114 filename = fname ;
66115 if ( Path . GetFileName ( filename ) . ToLower ( ) . Contains ( ".sql" ) )
67116 {
@@ -127,8 +176,6 @@ private void FolderPath_KeyDown(object sender, KeyEventArgs e)
127176 // 讀取資料夾中檔案名稱
128177 foreach ( string fname in Directory . GetFiles ( filepath ) )
129178 {
130- filename = "" ;
131- //filename = filename + fname + "\r\n";
132179 filename = fname ;
133180 if ( Path . GetFileName ( filename ) . ToLower ( ) . Contains ( ".sql" ) )
134181 {
@@ -157,24 +204,16 @@ private void OutputScripts_Click(object sender, EventArgs e)
157204 {
158205 sqlText += $@ ":r { FolderPath . Text } { "\\ " } { str } { "\n " } "; //寫入sql字串資料
159206 }
160- // 判斷檔案是否存在
161207 string FileName = saveFileDialog . FileName ;
162- if ( ! File . Exists ( FileName ) )
208+
209+ // 判斷檔案是否存在
210+ if ( File . Exists ( FileName ) )
163211 {
164- using ( StreamWriter writetext = new StreamWriter ( FileName , true , Encoding . Default ) )
165- {
166- writetext . WriteLine ( "" ) ;
167- }
212+ //清空文件
213+ ContentClear ( saveFileDialog . FileName ) ;
168214 }
169- //清空文件
170- FileStream fs ;
171- fs = new FileStream ( saveFileDialog . FileName , FileMode . Truncate , FileAccess . Write ) ; //Truncate模式打開文件可以清空。
172- fs . Close ( ) ;
173215 //寫入script
174- using ( StreamWriter writetext = new StreamWriter ( saveFileDialog . FileName , true , Encoding . Default ) )
175- {
176- writetext . WriteLine ( sqlText ) ;
177- }
216+ ContentWrite ( saveFileDialog . FileName , sqlText ) ;
178217 }
179218 }
180219
@@ -253,60 +292,50 @@ private void DBConnect_Click(object sender, EventArgs e)
253292 private void RunScript_Click ( object sender , EventArgs e )
254293 {
255294 string sqlText = "" ;
256- string readtext = "" ;
257- string FilePath = $@ "{ Directory . GetCurrentDirectory ( ) } { "\\ " } ";
258- DialogResult result = new DialogResult ( ) ;
259295
260- //判斷資料夾路徑是否存在
261- if ( Directory . Exists ( FolderPath . Text ) )
296+ //判斷資料夾路徑是否存在
297+ if ( Directory . Exists ( FolderPath . Text ) )
262298 {
263299 foreach ( var str in FileCheckedList . CheckedItems )
264300 {
265301 sqlText += $@ ":r { FolderPath . Text } { "\\ " } { str } { "\n " } ";
266302 }
267- //儲存清單.sql
268- using ( StreamWriter writetext = new StreamWriter ( $@ "{ FilePath } { "list.sql" } ", true , Encoding . Default ) )
303+
304+ if ( File . Exists ( $@ "{ FilePath } { "list.sql" } ") )
269305 {
270- writetext . WriteLine ( sqlText ) ;
271- sqlText = "" ;
306+ //清空文件
307+ ContentClear ( $@ " { FilePath } { "list.sql" } " ) ;
272308 }
273- //讀取 一次執行資料夾內所有的Script 文字
274- using ( StreamReader reader = new StreamReader ( $@ "{ FilePath } { "sample.bat" } ", Encoding . Default ) )
275- {
276- readtext = reader . ReadToEnd ( ) ;
277- }
278309
279- //設定bat檔內的資練庫連線資訊
310+ //儲存清單.sql
311+ ContentWrite ( $@ "{ FilePath } { "list.sql" } ", sqlText ) ;
312+ //讀取 一次執行資料夾內所有的Script 文字
313+ string readtext = ContentRead ( $@ "{ FilePath } { "sample.bat" } ") ;
314+
315+ //設定bat檔內的資練庫連線資訊
280316 readtext = readtext . Replace ( "dbIp=*" , $@ "dbIp={ DBLocation . Text } ") ;
281317 readtext = readtext . Replace ( "dbName=*" , $@ "dbName={ DBName . Text } ") ;
282318 readtext = readtext . Replace ( "dbUsrAcc=*" , $@ "dbUsrAcc={ UserName . Text } ") ;
283319 readtext = readtext . Replace ( "dbUsrPwd=*" , $@ "dbUsrPwd={ password . Text } ") ;
284320 readtext = readtext . Replace ( "batchFilePath=\" \" " , $@ "batchFilePath=""{ FilePath } """);
285321 readtext = readtext.Replace(" dbSqlFilePath= \" \" ", $@ "dbSqlFilePath=""{ FilePath } { "list.sql" } """);
286322
287- // 判斷檔案是否存在
288323 string FileName = $@" { FilePath} { "RunScripts.bat" } ";
289- if ( ! File . Exists ( FileName ) )
324+
325+ // 判斷檔案是否存在
326+ if ( File . Exists ( FileName ) )
290327 {
291- using ( StreamWriter writetext = new StreamWriter ( FileName , true , Encoding . Default ) )
292- {
293- writetext . WriteLine ( "" ) ;
294- }
328+ //清空文件
329+ ContentClear ( FileName ) ;
295330 }
296- //清空文件
297- FileStream fs ;
298- fs = new FileStream ( $@ "{ FilePath } { "RunScripts.bat" } ", FileMode . Truncate , FileAccess . Write ) ; //Truncate模式打開文件可以清空。
299- fs . Close ( ) ;
331+
300332 //寫入欲執行bat
301- using ( StreamWriter writer = new StreamWriter ( $@ "{ FilePath } { "RunScripts.bat" } ", true , Encoding . Default ) )
302- {
303- writer . WriteLine ( readtext ) ;
304- }
305- result = MessageBox . Show ( "是否執行勾選的script?" , "提示" , MessageBoxButtons . YesNo ) ;
306- if ( result == DialogResult . Yes )
333+ ContentWrite ( FileName , readtext ) ;
334+ DialogResult result = MessageBox . Show ( "是否執行勾選的script?" , "提示" , MessageBoxButtons . YesNo ) ;
335+ if ( result == DialogResult . Yes )
307336 {
308337 //如果log資料夾不存在就自動生成
309- if ( ! Directory . Exists ( ( $@ " { FilePath } { " \\ " } { "log" } " ) ) )
338+ if ( ! Directory . Exists ( LogPath ) )
310339 {
311340 Directory . CreateDirectory ( "log" ) ;
312341 }
@@ -320,7 +349,7 @@ private void RunScript_Click(object sender, EventArgs e)
320349 }
321350 else
322351 {
323- MessageBox . Show ( "資料夾 路徑錯誤 " , "提示" ) ;
352+ MessageBox . Show ( "資料夾路徑錯誤 " , "提示" ) ;
324353 }
325354 }
326355
0 commit comments