Last month my ISP contacted me about replacing my current modem with a new model. The replacement was free but required to change the IP.
I thought, “great, again going through all DNS settings for my domains”. For example: Microsoft DNS Server is storing IP entires in plain text files (ending with the extension “dns”).
I had an idea, to write an application for this purpose. I went ahead and programmed a simple tool. Its job was to allow folder selection and to replace a search query; its purpose was to do the same as notepad; using the replace option but processing multiply files. Later I was enhancing the program by using multi threading.
Operating System programmed on:
Windows 2000
Visual Studio C# Standard 2003Everything is starting by selecting a Folder. Code snipped:FileInfo [] fI;
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult A=this.folderBrowserDialog1.ShowDialog();
if(DialogResult.OK==A){
System.IO.DirectoryInfo D = new DirectoryInfo(this.folderBrowserDialog1.SelectedPath);
fI = D.GetFiles(this.TB_extension.Text);
this.tempFolder = this.folderBrowserDialog1.SelectedPath;
MessageBox.Show(fI.GetLength(0).ToString()+” File(s) found.”);
if(fI.GetLength(0)>0) this.B_Process.Enabled=true;
}
}
Then it is starting to handle the threads (void).
System.Threading.Thread a;
a = new Thread(new ThreadStart(handlingThreads));
a.Start();
I was using a Hashtable, to store the file location and to let the processing thread know what it should work on.
Hashtable CurrentThreads = new Hashtable();
It is important to limit the number of threads executed the same time. Thus, the application can run safe; it is not over loading the CPU.
void handlingThreads(){
System.Collections.IEnumerator Enumerator = fI.GetEnumerator();
lock(pB){
this.pB.Maximum = fI.GetLength(0)+2;
this.pB.Visible=true;
}
this.T_Query=this.TB_Query.Text;
this.T_Replace=this.TB_Replace.Text;
bool ok;
while(ok = Enumerator.MoveNext()){
Thread currentP = new Thread(new ThreadStart(CurrentFile));
this.CurrentThreads.Add(currentP,Enumerator.Current); // Store file location in Hastable
currentP.Start(); // Start Thread
Thread.Sleep(1); // Pause the thread. If you leave this out your CPU would get a usage of 100% when handling many files
while(this.CurrentThreads.Count>5){
Thread.Sleep(10); // Queue the processing when more than 5 threads are running the same time
}
}
int count=0;
while(CurrentThreads.Count>0&&!this.cancle) // Wait till all threads are closed. (sure is sure)
{
if(count>30) Thread.Sleep(10); else Thread.Sleep(10000); count++; // Don’t wait forever.
}
this.pB.Step=this.pB.Maximum;
MessageBox.Show(this,”Done!”,”",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.pB.Visible=false;
this.pB.Value = 0;
}
The final step to search, find and replace:
public void CurrentFile(){
string cF;
try{
cF = this.CurrentThreads[Thread.CurrentThread].ToString();
}
catch(System.Exception ex){
lock(errors)
{
errors+=ex.ToString()+”\n”;
}
return;
}
try{
string oFile = this.tempFolder + @”\” + cF; //Enumerator.Current;
StreamReader q = File.OpenText(oFile);
string tempContent = q.ReadToEnd();
q.Close();
q=null;
tempContent = Regex.Replace(tempContent, T_Query,Regex.Unescape(T_Replace));
StreamWriter q2 = File.CreateText(oFile);
q2.Write(tempContent);
q2.Close();
q2=null;
System.Threading.Thread.Sleep(10);
}
catch(System.Exception ex){
lock(errors)
{
errors+=ex.ToString()+”\n”;
}
}
finally
{
RemoveThread(Thread.CurrentThread);
}
}
You can download the entire project ZIP file here: http://blog.manueladam.com/download/FCR.zip
License: Free, Link to us.
Author: Manuel ADAM