Skip to main content
Tweeted twitter.com/StackCodeReview/status/758637956756299776
deleted 88 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

C# code that searches Searching files in a directory for a string

I'm a newbie at C# and would like input from more experienced programmers please. I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought i dontI don't explicitly specify that) for a given string and returns an IEnumerableIEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and iI think it could be faster. Perhaps i couldCould I use a better algorithm, or is there a better method of achieving this?

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}

C# code that searches files in a directory for a string

I'm a newbie at C# and would like input from more experienced programmers please. I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought i dont explicitly specify that) for a given string and returns an IEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and i think it could be faster. Perhaps i could use a better algorithm or is there a better method of achieving this?

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}

Searching files in a directory for a string

I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought I don't explicitly specify that) for a given string and returns an IEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and I think it could be faster. Could I use a better algorithm, or is there a better method of achieving this?

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}
deleted 35 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

C# - How to optimize code that reads textsearches files in a directory for a string

imI'm a newbie at C# and would like input from more experienced programmers please. I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought i dont explicitly specify that) for a given string and returns an IEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and i think it could be faster. Perhaps i could use a better algorithm or is there a better method of achieving this?

All help is appreciated. Thanks

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}

C# - How to optimize code that reads text files

im a newbie at C# and would like input from more experienced programmers please. I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought i dont explicitly specify that) for a given string and returns an IEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and i think it could be faster. Perhaps i could use a better algorithm or is there a better method of achieving this?

All help is appreciated. Thanks

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}

C# code that searches files in a directory for a string

I'm a newbie at C# and would like input from more experienced programmers please. I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought i dont explicitly specify that) for a given string and returns an IEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and i think it could be faster. Perhaps i could use a better algorithm or is there a better method of achieving this?

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}
Source Link
Nick
  • 191
  • 2
  • 6

C# - How to optimize code that reads text files

im a newbie at C# and would like input from more experienced programmers please. I have the following static class that enumerates directories in a folder, then searches each file in the folder (it seems to only work with text files even thought i dont explicitly specify that) for a given string and returns an IEnumerable that holds the results.

It takes about 15 seconds to go through 40 text files that are about 250kb in size and i think it could be faster. Perhaps i could use a better algorithm or is there a better method of achieving this?

All help is appreciated. Thanks

public static class LogFileReader
{
    public static IEnumerable<string> GetLines(string path, string searchterm)
    {

        var dirs = Directory.EnumerateDirectories(path);
        List<string> thelines = new List<string>();

        foreach (var dir in dirs)
        {

            var files = Directory.EnumerateFiles(dir);


            foreach (var file in files)
            {
                using (StreamReader sr = new StreamReader(file))
                {
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(searchterm, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            thelines.Add(line);
                        }
                    }

                }
            }

        }

        return thelines;
    }
}