Friday, September 17, 2010

process all the files under a directory & its sub-directories, C#

public void processDir(string sourceDir, string searchForText, string replaceWithText)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
            {
                // do something with fileName
                Console.WriteLine(fileName);
            }
            // Recurse into subdirectories of this directory.
            string[] subdirEntries = Directory.GetDirectories(sourceDir);
            foreach (string subdir in subdirEntries)
            // Do not iterate through reparse points
            if ((File.GetAttributes(subdir) &
                FileAttributes.ReparsePoint) !=
                FileAttributes.ReparsePoint)
            {
                processDir(subdir, searchForText, replaceWithText);//recursive function
                recursionLvl += 1;
                Console.write ("Changing at deepness level : " + recursionLvl.ToString() + "\n");
            }

using Regular Expressions in C# to find a phrase in a text and replace it with another

This is an example on how to find & replace a phrase in a string, the easy way in C# :

            
            using System.Text.RegularExpressions;
            ....
            if (Regex.IsMatch(content, searchText, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                display.Text += "Found match in " + filePath +" ***\n";
                content = Regex.Replace(content, searchText, replaceText);
            }

folderBrowserDialog, C#

Example on how to get a path to work on it later on from folderBrowserDialog in C#

          DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                thePath = folderBrowserDialog1.SelectedPath;
                pathLabel.Text = folderBrowserDialog1.SelectedPath;
            }

Find & Replace requested text in all files application

This small C#.Net application finds & replace the requested text with desired text in all files under one folder & sub-folders. This application was made according to needs of some changes at webpages at work.
The application is under development to be stable enough for release ...

Tuesday, September 14, 2010

HOWTO, uneditable text box in C#

If you want a uneditable text box, then ReadOnly properties on the text box should be set as True.

Wednesday, September 08, 2010

Different arrays in C#, Jagged vs. Multi dimensional

Jagged array :
bool[][] myBools = new bool[2][];


Multi dimentional array : 
double[,] myDoubles = new double[2, 2];


One of the differences between jagged arrays and a multi-dimension array is that a multi-dimension array will allocate memory for every element of each dimension, whereas a jagged array will only allocate memory for the size of each array in each dimension that you define. Most of the time, you'll be using multi-dimension arrays, if you need multiple dimensions, and will only use jagged arrays in very special circumstances when you are able to save significant memory by explicitly specifying the sizes of the arrays in each dimension.