Skip to main content
added 26 characters in body
Source Link
paparazzo
  • 6.1k
  • 3
  • 20
  • 43

No need to put all the file names in an array first. Just process them.

If you really want async it is supported by StreamReader and TextReader.

From DirectoryInfo to FileInfo you can open the file directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}

No need to put all the file names in an array first. Just process them.

If you really want async it is supported by StreamReader and TextReader.

From FileInfo you can open directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}

No need to put all the file names in an array first. Just process them.

If you really want async it is supported by StreamReader and TextReader.

From DirectoryInfo to FileInfo you can open the file directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}
added 172 characters in body
Source Link
paparazzo
  • 6.1k
  • 3
  • 20
  • 43

No need to put all the file names in an array first. Just process them.

If you really want async it is supported by StreamReader and TextReader.

From FileInfo you can open directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}

No need to put all the file names in an array first. Just process them.

From FileInfo you can open directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}

No need to put all the file names in an array first. Just process them.

If you really want async it is supported by StreamReader and TextReader.

From FileInfo you can open directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}
Source Link
paparazzo
  • 6.1k
  • 3
  • 20
  • 43

No need to put all the file names in an array first. Just process them.

From FileInfo you can open directly.

Line by line will have a lower memory usage.

public static void ListText(string DirName = @"c:\temp\")
{
    DirectoryInfo di = new DirectoryInfo(DirName);
    if (di != null && di.Exists)
    {
        foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
        {
            //Debug.WriteLine(fi.Extension);
            //Debug.WriteLine(fi.FullName);
            if (   string.Compare(fi.Extension, ".txt", true) == 0
                || string.Compare(fi.Extension, ".text", true) == 0)
            {
                using (StreamReader sr = fi.OpenText())
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if(!string.IsNullOrEmpty(s))
                        {
                            Debug.WriteLine(s); 
                        }
                    }
                }
            }
        }
    }
}