how can I apply to all txt files in the folder. (save to a different folder the same name all the txt files).
this code is only working for a txt file but I want to apply all txt files in a folder its.
I have following code:

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class MainClass
{
    public static void Main(string[] args)
    {
            string inFile, filePath;
            string line;
            Console.WriteLine("Input file name to open!");
            Console.WriteLine("Sample: d:\\filename.txt");
            inFile = Convert.ToString(Console.ReadLine());
            StreamReader sr = File.OpenText(inFile);
            line = sr.ReadLine();

            Console.WriteLine();
            Console.WriteLine("Successful file open!...");
            Console.WriteLine();
            Console.WriteLine("Input file name to save!");
            Console.WriteLine("sample: d:\\filename.txt");
            filePath = Console.ReadLine();
            FileStream file = new FileStream(filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(file);


            List<string> lines = new List<string>();
            //add all the lines to a list
            while ((line = sr.ReadLine()) != null)
            {
                lines.Add(line);
            }
            //read the lines you would like to read:
            try
            {
                int numberOne = Int32.Parse(lines[0]);
                int numberTwo = Int32.Parse(lines[1]);
                int numberThree = Int32.Parse(lines[2]);
                int x = numberOne + numberTwo + numberThree;
                sw.WriteLine(x);
            }
            catch { }
            Console.ReadLine();
            sr.Close();
            sw.Close();
    }
}

please can you show on the code?

You can use this to iterate each text file to perform your operation:

DirectoryInfo di = new DirectoryInfo(@"C:\somedir\");
FileInfo[] files = di.GetFiles("*.txt");
foreach (FileInfo fi in files)
{
  string inFile = fi.FullName;
  //do work here
}

how can I put in process simultaneously all the txt files in folder.Also I want to save a different folder the same name all the txt files.

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class MainClass
{
    public static void Main(string[] args)
    {
        string inFile, filePath;
        string line;


        DirectoryInfo di = new DirectoryInfo(@"D:\a\");
        FileInfo[] files = di.GetFiles("*.txt");
        Console.WriteLine("Input file name to open!");
        Console.WriteLine("Sample: d:\\filename.txt");
        inFile = Convert.ToString(Console.ReadLine());
        StreamReader sr = File.OpenText(inFile);
        line = sr.ReadLine();
        foreach (FileInfo fi in files)
        {
            inFile = fi.FullName;
           
            Console.WriteLine();
            Console.WriteLine("Successful file open!...");
            Console.WriteLine();
            Console.WriteLine("Input file name to save!");
            Console.WriteLine("sample: d:\\filename.txt");
            filePath = Console.ReadLine();
            FileStream file = new FileStream(filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(file);
            List<string> lines = new List<string>();

            while ((line = sr.ReadLine()) != null)
            {
                lines.Add(line);
            }
            try
            {
                int numberOne = Int32.Parse(lines[0]);
                int numberTwo = Int32.Parse(lines[1]);
                int numberThree = Int32.Parse(lines[2]);
                int x = numberOne + numberTwo + numberThree;
                sw.WriteLine(x);
            }
            catch { }
            sw.Close();
            sr.Close();
        }

    }
}

What are you trying to accomplish and what are the contents of these text files? Since the FilePath is read from user input i'm guessing you want a different directory provided by hand for each of the files?

Are these text files 3 lines, and each line is a number, and you want to add the 3 lines and write the value to a 4th line?

sample D:\\A I want to call the A folder.
in A:
1.txt
5
4
9
2.txt
8
7
12
...
After I put in process the A folder I want to save a different folder( sample D:\\B) the results.
in B:
1.txt
18
2.txt
27
...

private void simpleButton4_Click(object sender, EventArgs e)
    {
      const string inDir = "C:\\data\\A\\";
      const string outDir = "C:\\data\\B\\";
      DirectoryInfo di = new DirectoryInfo(inDir);
      FileInfo[] files = di.GetFiles("*.txt");
      foreach (FileInfo fi in files)
      {
        int result = 0;
        using (StreamReader sr = new StreamReader(fi.FullName))
        {
          string s;
          while ((s = sr.ReadLine()) != null)
          {
            int iValue;
            if (int.TryParse(s, out iValue))
              result += iValue;
          }
          sr.Close();
        }
        string destFile = Path.Combine(outDir, fi.Name);
        if (File.Exists(destFile))
          File.Delete(destFile);

        File.WriteAllText(destFile, Convert.ToString(result));
      }
    }

Scott Knake A thousand times thank you!

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class MainClass
{
    public static void Main(string[] args)
    {
        string inFile;
        string line;

        const string inDir = "D:\\data\\A\\";
        const string outDir = "D:\\data\\B\\";
        DirectoryInfo di = new DirectoryInfo(inDir);
        FileInfo[] files = di.GetFiles("*.txt");
        foreach (FileInfo fi in files)
        {
            inFile = fi.FullName;
            StreamReader sr = File.OpenText(inFile);
            List<string> lines = new List<string>();

            while ((line = sr.ReadLine()) != null)
            {
                lines.Add(line);
            }
            
                int numberOne = Int32.Parse(lines[0]);
                //int numberTwo = Int32.Parse(lines[1]);
                int numberThree = Int32.Parse(lines[2]);
                //int x = numberOne + numberTwo + numberThree;
                int x = numberOne + numberThree;

            sr.Close();
            string destFile = Path.Combine(outDir, fi.Name);
            File.WriteAllText(destFile, Convert.ToString(x));
        }

    }
}
This question has already been answered. Start a new discussion instead.