Skip to content

Commit 08b91da

Browse files
committed
Fix logic and fasten up the process
1 parent a8640e4 commit 08b91da

File tree

8 files changed

+30
-33
lines changed

8 files changed

+30
-33
lines changed

.vs/LFAR/v16/.suo

2.5 KB
Binary file not shown.
0 Bytes
Binary file not shown.

LFAR/Form1.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

604 Bytes
Binary file not shown.

LFAR/obj/Debug/LFAR.csproj.FileListAbsolute.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ D:\LinesFindAndReplace\LFAR\obj\Debug\LFAR.Properties.Resources.resources
1313
D:\LinesFindAndReplace\LFAR\obj\Debug\LFAR.csproj.GenerateResource.cache
1414
D:\LinesFindAndReplace\LFAR\obj\Debug\LFAR.exe
1515
D:\LinesFindAndReplace\LFAR\obj\Debug\LFAR.pdb
16+
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\bin\Debug\LFAR.exe.config
1617
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\bin\Debug\LFAR.exe
1718
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\bin\Debug\LFAR.pdb
18-
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\obj\Debug\LFAR.csprojAssemblyReference.cache
1919
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\obj\Debug\LFAR.Form1.resources
2020
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\obj\Debug\LFAR.Properties.Resources.resources
2121
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\obj\Debug\LFAR.csproj.GenerateResource.cache
2222
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\obj\Debug\LFAR.exe
2323
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\obj\Debug\LFAR.pdb
24-
C:\Users\artis\Downloads\LinesFindAndReplace\LFAR\bin\Debug\LFAR.exe.config
-13.5 KB
Binary file not shown.

LFAR/obj/Debug/LFAR.exe

0 Bytes
Binary file not shown.

LFAR/obj/Debug/LFAR.pdb

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)