@@ -106,7 +106,7 @@ private void Form1_Load(object sender, EventArgs e)
106106 // focus on last row
107107 try { dataGridView1 . CurrentCell = dataGridView1 . Rows [ dataGridView1 . RowCount - 1 ] . Cells [ 0 ] ; } catch { }
108108
109- label1 . Text = "Total: " + dataGridView1 . RowCount . ToString ( ) + " lines" ;
109+ label1 . Text = "Total: " + dataGridView1 . RowCount . ToString ( ) + " lines to replace " ;
110110 }
111111
112112 private void deleteCollectionButton_Click ( object sender , EventArgs e )
@@ -189,51 +189,49 @@ private void buttonSelectPathOfFile(object sender, EventArgs e)
189189
190190 private async void buttonDoReplaceJob_Click ( object sender , EventArgs e )
191191 {
192- label1 . Text = "Running task, please wait..." ;
193-
194- int replacesCount = await Task . Run ( ( ) => DoReplaceJob ( 3000 ) ) ;
192+ int replacesCount = 0 ;
195193
196- label1 . Text = "Jobs finished: " + replacesCount + " replaces." ;
194+ DialogResult result = MessageBox . Show ( "This action will replace all lines with the values specified." , "Confirmation" , MessageBoxButtons . YesNo ) ;
195+ if ( result == DialogResult . Yes )
196+ {
197+ if ( File . Exists ( pathOfFile ) )
198+ {
199+ label1 . Text = "Running task, please wait..." ;
200+ replacesCount = await Task . Run ( ( ) => DoReplaceJob ( 1000 ) ) ;
201+ label1 . Text = "Jobs finished: " + replacesCount + " replaces." ;
202+ }
203+ else
204+ MessageBox . Show ( "Please select a valid file!" ) ;
205+ }
197206 }
198207
199208 private int DoReplaceJob ( int sleepTime )
200209 {
201210 int replacesCount = 0 ;
211+ string text = string . Empty ;
202212
203- if ( File . Exists ( pathOfFile ) )
213+ using ( StreamReader sr = new StreamReader ( pathOfFile ) )
204214 {
205- DialogResult result = MessageBox . Show ( "This action will replace all lines with the values specified." , "Confirmation" , MessageBoxButtons . YesNo ) ;
206- if ( result == DialogResult . Yes )
215+ int i = 0 ;
216+ do
207217 {
218+ i ++ ; string line = sr . ReadLine ( ) ;
219+
208220 foreach ( DataGridViewRow row in dataGridView1 . Rows )
209221 {
210- // find and replace line from datagridview
211- string text = "" ;
212- using ( StreamReader sr = new StreamReader ( pathOfFile ) )
222+ if ( line . Contains ( row . Cells [ 1 ] . Value . ToString ( ) ) )
213223 {
214- int i = 0 ;
215- do
216- {
217- i ++ ;
218- string line = sr . ReadLine ( ) ;
219-
220- if ( line == row . Cells [ 1 ] . Value . ToString ( ) )
221- replacesCount ++ ;
222-
223- if ( line == row . Cells [ 1 ] . Value . ToString ( ) )
224- line = line . Replace ( line , row . Cells [ 2 ] . Value . ToString ( ) ) ;
225-
226- text = text + line + Environment . NewLine ;
227-
228- } while ( sr . EndOfStream == false ) ;
224+ line = line . Replace ( line , row . Cells [ 2 ] . Value . ToString ( ) ) ;
225+ replacesCount ++ ;
229226 }
230- if ( replacesCount > 0 )
231- File . WriteAllText ( pathOfFile , text ) ;
232227 }
233- }
228+ text = text + line + Environment . NewLine ;
229+
230+ } while ( sr . EndOfStream == false ) ;
234231 }
235- else
236- MessageBox . Show ( "Please select a valid file!" ) ;
232+
233+ if ( replacesCount > 0 )
234+ File . WriteAllText ( pathOfFile , text ) ;
237235
238236 Thread . Sleep ( sleepTime ) ;
239237
0 commit comments