Text reading and string inputting program The Main program. This program pulls from a .txt document with two separate strings, and outputs a  .cs program that will display those two string via console when run. It uses a structure.txt file for the code it inputs the strings into.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {


        //structure.txt contains the program we will enter our values into.
        String filePath = "Structure.txt";

        WriteToFile(filePath);



    }

    public static void WriteToFile(string filePath)
    {
        //create a string array to gather our text file information.

        StreamReader reader = new StreamReader(filePath);
        StreamReader info = new StreamReader("Structure.txt");

        StreamWriter writer = new StreamWriter("Hello.cs", true);
        String temp = String.Empty;

        while (!info.EndOfStream)
        {
            String tempstring = String.Empty;
            tempstring = reader.ReadLine();

            while (!reader.EndOfStream)
            {

                temp = reader.ReadLine();
                writer.WriteLine(temp);
                if (temp == "//break")
                {
                    writer.WriteLine($"String1 = {tempstring}");

                }
                else if (temp == "//break 2") {
                    writer.WriteLine($"String2 = {tempstring}");
                }

            }
        }
        reader.Close();
        info.Close();


        writer.Close();
    }

}
}


structure.txt is just a basic program that prints the two strings gathered from the main program unto the console.

using System;
namespace HelloWorld
{
class Hello 
{
    static void Main() 
    {
//break

//break 2

    Console.WriteLine(string1);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();

    Console.WriteLine(string2);
    Console.ReadKey();
    }
}
//end
}


This program runs, but I am wanting to improve the code and increase its efficiency.
I wonder if you even have tested this program:

This:


  while (!info.EndOfStream)
  {
    String tempstring = String.Empty;
    tempstring = reader.ReadLine();

    while (!reader.EndOfStream)
    {



will run forever because info is never read from and will never reach EOS.



In the context this:


   while (!info.EndOfStream)    {
     String tempstring = String.Empty;
     tempstring = reader.ReadLine();



is not understandable. Do you have the tempstring that is going to be written instead of the placeholders in the top of the structure.txt-file?

You open the same file twice:


public static void WriteToFile(string filePath)
{
  //create a string array to gather our text file information.

  StreamReader reader = new StreamReader(filePath);
  StreamReader info = new StreamReader("Structure.txt");



because the argument filePath is also "Structure.txt". Why open the same file more than once for the same operation?



StreamReader and StreamWriter implement IDisposable and should be disposed off like:

using (StreamReader reader = new StreamReader(filePath))
using (StreamWriter writer = new StreamWriter("helle.cs"))
{
   ...
}




You shouldn't hardcode string literals in a method but provide them as arguments:

public static void WriteToFile(string inputFile, string outputFile)
{
   ...
}




Instead of:


 while (!reader.EndOfStream)
 {

    temp = reader.ReadLine();



it is better to do:

string line = null;
while ((line = reader.ReadLine()) != null)
{
   ...
}





  StreamWriter writer = new StreamWriter("Hello.cs", true);


you set the append argument to true, but I'm quite sure you mean to overwrite the existing file?