Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Replace text in files class

Hello everyone,

So I had to make a little program for a test on school.
And this program needed to replace text in files.
You could choose between directories/subdirectories etc.
And some of you might need some help with that. So I decided to post it on HF.

Here is the class:

Code
public class FileReplace
    {
        public delegate void FileProcessed(string fileName, string replace, Dictionary<int, string> linesReplaced);
        public event FileProcessed FileProcessedHandler;

        public void Replace(string search, string replace, string path, bool subDirectories = false, bool caseSensitive = false)
        {
            if (Directory.Exists(path) || File.Exists(path))
            {
                if (Path.HasExtension(path))
                {
                    ReplaceFile(path, search, replace, caseSensitive);
                }
                else
                {
                    var files = Directory.GetFiles(path, "*.*", subDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
                    foreach(var file in files)
                    {
                        ReplaceFile(file, search, replace, caseSensitive);
                    }
                }
            }
        }


        private void ReplaceFile(string path, string search, string replace, bool caseSensitive)
        {
            if (File.Exists(path))
            {
                var file = File.ReadAllLines(path);
                search = caseSensitive ? path.ToLower() : search;
                var linesReplaced = new Dictionary<int, string>();
                for (int l = 0; l < file.Length; l++)
                {
                    var line = caseSensitive ? file[l].ToLower() : file[l];
                    if (line.Contains(search))
                    {
                        file[l] = file[l].Replace(search, replace);
                        linesReplaced.Add(l, file[l]);
                    }
                }
                File.WriteAllLines(path, file);
                FileProcessedHandler(path, replace, linesReplaced);
            }
        }

    }

Please say what I could improve or what I did good 😊
Your case insensitive search makes the entire line lowercase. You could do something like this:

Spoiler (Click to View)
Lotta blood to let, peace to make, give a fuk about a check
(Jan 16, 2017 04:30 AM)BahNahNah Wrote:  Your case insensitive search makes the entire line lowercase. You could do something like this:

Spoiler (Click to View)

Waittt... I feel like I remember you bitching about same-line brackets. 😝
(Jan 16, 2017 06:52 PM)Hawk HF Wrote:  Waittt... I feel like I remember you bitching about same-line brackets. 😝

Ikr.

I was ment to work on a project with someone that would only use same line brackets, so I switched vs to it and never changed it back. + that I'm learning golang which won't compile unless the brackets are on the same line.

There is an argument for both styles, but I do like the compactness of same line.
Lotta blood to let, peace to make, give a fuk about a check
(Jan 16, 2017 06:59 PM)BahNahNah Wrote:  Ikr.

I was ment to work on a project with someone that would only use same line brackets, so I switched vs to it and never changed it back. + that I'm learning golang which won't compile unless the brackets are on the same line.

There is an argument for both styles, but I do like the compactness of same line.

I've actually switched to new line brackets for the same reason lol.
It's just a standard where I work.
(Jan 16, 2017 04:30 AM)BahNahNah Wrote:  Your case insensitive search makes the entire line lowercase. You could do something like this:

Spoiler (Click to View)

No you should not do some thing like that fucktard. Lets ignore the fact it's to hard for you to post in the correct section.

Do not try to teach others when you have not even read bloody msdn for the methods you use. String.Equals Method (String, StringComparison) is already overloaded to handle case. Try reading first https://msdn.microsoft.com/en-us/library....110).aspx

You also use tolower and have no idea what its meant for....


As explained in Best Practices for Using Strings in the .NET Framework, we recommend that you avoid calling string casing methods that substitute default values and instead call methods that require parameters to be explicitly specified. To convert a character to lowercase by using the casing conventions of the current culture, call theToLower(CultureInfo) method overload with a value of CultureInfo.CurrentCulture for its culture parameter.



But how can we expect any thing from you when you cant post VB in a vb section.
(Jan 17, 2017 07:46 PM)Sn0wX Wrote:  No you should not do some thing like that fucktard. Lets ignore the fact it's to hard for you to post in the correct section.

Do not try to teach others when you have not even read bloody msdn for the methods you use. String.Equals Method (String, StringComparison) is already overloaded to handle case. Try reading first https://msdn.microsoft.com/en-us/library....110).aspx

You also use tolower and have no idea what its meant for....

But how can we expect any thing from you when you cant post VB in a vb section.

I was not aware that string.Equals supported case insensitive comparison, so thanks for pointing that out.

I diddnt say should, i said could. Im very aware that my code is not optimized, it was just concept code to present other ways of doing things.

OPs code was c#, so i provided c#.
Lotta blood to let, peace to make, give a fuk about a check
(Jan 16, 2017 04:30 AM)BahNahNah Wrote:  Your case insensitive search makes the entire line lowercase. You could do something like this:

Spoiler (Click to View)

Don't you mean if(CaseInsensitive) ?
[Image: Syr3DlV.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 145 Apr 22, 2021 07:10 AM
Last Post: lockhead
[C#] by lUL280yJul 4, 2019 07:51 AM 5 194 Jul 4, 2019 07:51 AM
Last Post: eCoLoGy
[C] by ColossalOctopusMar 14, 2019 09:03 AM 2 162 Mar 14, 2019 09:03 AM
Last Post: 0xDEAD10CC