Concatenate text files with c #?

0

I have an exercise in which they ask me to concatenate the content of the first and second text files through a function, saving the content in the first file, this is what I have done so far, but it does not work for me, ideas?

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

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            string nom1, nom2;
            Console.WriteLine("Entra la ruta o nom del primer arxiu");
            nom1 = Console.ReadLine();

            Console.WriteLine("Entra la ruta o nom del segon arxiu");
            nom2 = Console.ReadLine();

            inicia(nom1, nom2);
        }
        static void inicia(string nom1, string nom2)
        {
            try
            {
                using (StreamWriter sw = File.AppendText(nom1))
                {
                    using (StreamReader sr = new StreamReader(nom1))
                    {
                        string line;

                        while ((line = sr.ReadLine()) != null)
                        {
                            sw.Write(line);
                        }
                        using (StreamReader sr2 = new StreamReader(nom2))
                        {
                            string line2;

                            while ((line2 = sr2.ReadLine()) != null)
                            {
                                sw.Write(line2);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error general");
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

            Console.ReadKey();
        }


    }
}
    
asked by THR4SH3RP0L0 15.05.2017 в 20:06
source

2 answers

1

Good morning, you would have the following

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

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            string directorio1, directorio2;
            Console.WriteLine("Ingrese la ruta o nom del primer archivo");
            directorio1 = Console.ReadLine();

            Console.WriteLine("Ingrese la ruta o nom del segundo archivo");
            directorio2 = Console.ReadLine();

            Concatenar(directorio1, directorio2);
        }

        static void Concatenar(string directorio1, string directorio2)
        {
            try
            {
                string string1 = File.ReadAllText(directorio1);
                string string2 = File.ReadAllText(directorio2);
                File.WriteAllText(directorio1, string1 + "\n" + string2);
            }

            catch (Exception e)
            {
                Console.WriteLine("Error general");
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

            Console.ReadKey();
        }    
    }
}
    
answered by 15.05.2017 / 20:46
source
3

If they are text files, the easiest thing is something like this:

string contenidoPrimero = File.ReadAllText(@"c:\primero.txt");
string contenidoSegundo = File.ReadAllText(@"c:\segundo.txt");
File.WriteAllText(@"c:\juntos.txt", contenidoPrimero + contenidoSegundo);
    
answered by 15.05.2017 в 20:30