using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BackGroundWorkerSample
{
public partial class frmBackGroundWorker : Form
{
public frmBackGroundWorker()
{
InitializeComponent();
}
BackgroundWorker bgWorker = new BackgroundWorker();
private void btnStart_Click(object sender, EventArgs e)
{
string strFind = txtFind.Text;
string strReplace = txtReplace.Text;
string folderPath = txtFolderPath.Text;
string SearchInFiles = txtSearchInFiles.Text;
UseWaitCursor = true;
btnStart.Enabled = false;
bgWorker.RunWorkerAsync(new WorkInfo(strFind, strReplace, folderPath, SearchInFiles, this));
}
private void Form1_Load(object sender, EventArgs e)
{
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
}
void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
workerProgress.Value = e.ProgressPercentage;
}
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
int[] myResult = (int[]) e.Result;
foreach (int i in myResult)
txtResult.Text += i.ToString() + " ";
btnStart.Enabled = true;
UseWaitCursor = false;
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
WorkInfo info = (WorkInfo)e.Argument;
MyWorkerClass m = new MyWorkerClass(info,bgWorker, this);
e.Result = m.DoReplaceText();
}
private void btnStop_Click(object sender, EventArgs e)
{
bgWorker.CancelAsync();
UseWaitCursor = false;
btnStart.Enabled = true;
}
public void updateScreenControls(string strFileProcessedCount)
{
ttlFileProcessedCount.Text = strFileProcessedCount;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.IO;
namespace BackGroundWorkerSample
{
public class MyWorkerClass
{
BackgroundWorker bgWorker = null;
public string Find;
public string Replace;
public string FolderPath;
public string SearchInFiles;
public frmBackGroundWorker FrmBackGroundWorker;
public MyWorkerClass(WorkInfo info, BackgroundWorker bw, frmBackGroundWorker frmBackGroundWorker)
{
this.Find = info.Find;
this.Replace = info.Replace;
this.FolderPath = info.FolderPath;
this.SearchInFiles = info.SearchInFiles;
this.bgWorker = bw;
this.FrmBackGroundWorker = info.FrmBackGroundWorker;
}
public bool ReplaceText(ref string content, string oldValue, string newValue, ref int replaced)
{
Boolean isReplaced = false;
int startIndex = 0;
while (startIndex != -1)
{
startIndex = content.IndexOf(oldValue, startIndex);
if (startIndex != -1)
{
content = content.Remove(startIndex, oldValue.Length);
content = content.Insert(startIndex, newValue);
replaced += 1;
isReplaced = true;
}
}
return isReplaced;
}
public string DoReplaceText()
{
int replaced = 0;
int fileProcessed = 0;
int i = 0;
string strFind = this.Find;
string strReplace= this.Replace;
string strFolderPath = this.FolderPath;
string strSearchInFiles = this.SearchInFiles;
//Get all the files from the root directory filtered by a filter text.
string[] fileList = Directory.GetFiles(@"" + strFolderPath, strSearchInFiles, SearchOption.AllDirectories);
//Loop through each file, call the ReplaceText() method
//and replace the file if something was replaced.
foreach (string file in fileList)
{
i = i + 1;
StreamReader sr = new StreamReader(file);
string content = sr.ReadToEnd();
sr.Close();
sr.Dispose();
if (ReplaceText(ref content, strFind, strReplace, ref replaced))
{
StreamWriter sw = new StreamWriter(file);
sw.Write(content);
sw.Flush();
sw.Close();
sw.Dispose();
}
if (bgWorker.CancellationPending)
break;
//numbers.Add(i + (i + 1));
System.Threading.Thread.Sleep(15);
// Raise the ProgressChanged event and set the current progress value
bgWorker.ReportProgress((int)i);
fileProcessed = fileProcessed + 1;
//ttlFileProcessedCount
FrmBackGroundWorker.updateScreenControls(fileProcessed.ToString());
}
return "Total replacements = " + replaced + "" + " from files " + fileProcessed;
}
}
public class WorkInfo
{
public string Find;
public string Replace;
public string FolderPath;
public string SearchInFiles;
public frmBackGroundWorker FrmBackGroundWorker;
public WorkInfo(string strFind, string strReplace, string strFolderPath, string strSearchInFiles, frmBackGroundWorker ffrmBackGroundWorker)
{
this.Find = strFind;
this.Replace = strReplace;
this.FolderPath = strFolderPath;
this.SearchInFiles = strSearchInFiles;
this.FrmBackGroundWorker = ffrmBackGroundWorker;
}
}
}
ASKER
ASKER
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BackGroundWorkerSample
{
public partial class frmBackGroundWorker : Form
{
public frmBackGroundWorker()
{
InitializeComponent();
}
BackgroundWorker bgWorker = new BackgroundWorker();
private void btnStart_Click(object sender, EventArgs e)
{
string strFind = txtFind.Text;
string strReplace = txtReplace.Text;
string folderPath = txtFolderPath.Text;
string SearchInFiles = txtSearchInFiles.Text;
UseWaitCursor = true;
btnStart.Enabled = false;
bgWorker.RunWorkerAsync(new WorkInfo(strFind, strReplace, folderPath, SearchInFiles, this));
}
private void Form1_Load(object sender, EventArgs e)
{
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
}
void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
workerProgress.Value = e.ProgressPercentage;
}
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
int[] myResult = (int[]) e.Result;
foreach (int i in myResult)
txtResult.Text += i.ToString() + " ";
btnStart.Enabled = true;
UseWaitCursor = false;
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
WorkInfo info = (WorkInfo)e.Argument;
MyWorkerClass m = new MyWorkerClass(info,bgWorker);
e.Result = m.DoReplaceText();
}
private void btnStop_Click(object sender, EventArgs e)
{
bgWorker.CancelAsync();
UseWaitCursor = false;
btnStart.Enabled = true;
}
public void updateScreenControls(string strFileProcessedCount)
{
ttlFileProcessedCount.Text = strFileProcessedCount;
Application.DoEvents();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.IO;
namespace BackGroundWorkerSample
{
public class MyWorkerClass
{
BackgroundWorker bgWorker = null;
public string Find;
public string Replace;
public string FolderPath;
public string SearchInFiles;
public frmBackGroundWorker FrmBackGroundWorker;
public MyWorkerClass(WorkInfo info, BackgroundWorker bw)
{
this.Find = info.Find;
this.Replace = info.Replace;
this.FolderPath = info.FolderPath;
this.SearchInFiles = info.SearchInFiles;
this.bgWorker = bw;
this.FrmBackGroundWorker = info.FrmBackGroundWorker;
}
public bool ReplaceText(ref string content, string oldValue, string newValue, ref int replaced)
{
Boolean isReplaced = false;
int startIndex = 0;
while (startIndex != -1)
{
startIndex = content.IndexOf(oldValue, startIndex);
if (startIndex != -1)
{
content = content.Remove(startIndex, oldValue.Length);
content = content.Insert(startIndex, newValue);
replaced += 1;
isReplaced = true;
}
}
return isReplaced;
}
public string DoReplaceText()
{
int replaced = 0;
int fileProcessed = 0;
int i = 0;
string strFind = this.Find;
string strReplace= this.Replace;
string strFolderPath = this.FolderPath;
string strSearchInFiles = this.SearchInFiles;
//Get all the files from the root directory filtered by a filter text.
string[] fileList = Directory.GetFiles(@"" + strFolderPath, strSearchInFiles, SearchOption.AllDirectories);
//Loop through each file, call the ReplaceText() method
//and replace the file if something was replaced.
foreach (string file in fileList)
{
i = i + 1;
StreamReader sr = new StreamReader(file);
string content = sr.ReadToEnd();
sr.Close();
sr.Dispose();
if (ReplaceText(ref content, strFind, strReplace, ref replaced))
{
StreamWriter sw = new StreamWriter(file);
sw.Write(content);
sw.Flush();
sw.Close();
sw.Dispose();
}
if (bgWorker.CancellationPending)
break;
//numbers.Add(i + (i + 1));
System.Threading.Thread.Sleep(15);
// Raise the ProgressChanged event and set the current progress value
bgWorker.ReportProgress((int)i);
fileProcessed = fileProcessed + 1;
//ttlFileProcessedCount
FrmBackGroundWorker.updateScreenControls(fileProcessed.ToString());
}
return "Total replacements = " + replaced + "" + " from files " + fileProcessed;
}
}
public class WorkInfo
{
public string Find;
public string Replace;
public string FolderPath;
public string SearchInFiles;
public frmBackGroundWorker FrmBackGroundWorker;
public WorkInfo(string strFind, string strReplace, string strFolderPath, string strSearchInFiles, frmBackGroundWorker ffrmBackGroundWorker)
{
this.Find = strFind;
this.Replace = strReplace;
this.FolderPath = strFolderPath;
this.SearchInFiles = strSearchInFiles;
this.FrmBackGroundWorker = ffrmBackGroundWorker;
}
}
}
ASKER
ASKER
ASKER
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
Why not put all that code into the form itself? There will be less hassle since you won't have to pass all that info around...