-6

I want to get Text from some text files and insert it to a text box. I created some methods, which get the text of the files. My aim is to combine the text files to one text. the problem is that the program shows me only the last text file.

How can I combine all text files to one?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
grekko
  • 53
  • 1
  • 11

3 Answers3

3
  1. Create a StringBuilder object. http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
  2. Read each file and add the file contents to the StringBuilder using the Append method of the StringBuilder object
  3. Assign the Value of StringBuilder.ToString() to the TextBox.Text property
mortb
  • 9,361
  • 3
  • 26
  • 44
1

Without seeing your code this is the best I can do:

myTextBox.Text = 
  File.ReadAllText("file1") + 
  File.ReadAllText("file2") + 
  File.ReadAllText("file3");
Polyfun
  • 9,479
  • 4
  • 31
  • 39
  • For better performance, use StringBuilder instead of appending the strings with the + operator – mortb Jul 03 '12 at 09:22
  • I tried the StringBuilder but it isnt available for me. VS shows me only StringReader, StringWriter – grekko Jul 03 '12 at 09:29
  • @mortb - not necessarily. Like most things in life it depends, and in this case we do not have enough info to tell whether StringBuilder will be beneficial. See http://stackoverflow.com/questions/529999/when-to-use-stringbuilder for some guidelines. – Polyfun Jul 03 '12 at 09:30
  • @grekko - you need "using System.Text;" at the top of your file to get access to the StringBuilder class. – Polyfun Jul 03 '12 at 09:32
  • @ShellShock: Yeah I know. My angle on it is that the input is files which may be quite long and stringbuilder would improve performance (or short where stringbuidler does not.) – mortb Jul 03 '12 at 09:37
  • Sorry, I have following method: – grekko Jul 05 '12 at 07:53
0

I have following methods: (they are remotely the same)

public void Method1()
    {
        string variable1 = "$variable1";
        string variable2 = "$variable2";
        string file = System.IO.File.ReadAllText(@"C:\temp\textfile.txt", System.Text.Encoding.UTF8);

        try
        {
            if (File.Exists(@"C:\temp\textfile.txt"))
            {
                // NOP: Nothing to DO
            }
            if (file.Contains(variable1infile))
            {
                file = file.Replace(variable1, variable1infile);
                System.IO.File.WriteAllText(@"C:\temp\textfile.txt", file);
                var reload = File.ReadAllText(@"C:\temp\textfile.txt");
                TextBox1.Text = reload;
            }
            if (file.Contains(variable2infile))
            {
                file = file.Replace(variable2, variable2infile);
                System.IO.File.WriteAllText(@"C:\temp\textfile.txt", file);
                var reload = File.ReadAllText(@"C:\temp\textfile.txt");
                TextBox1.Text = reload;
            }
            var gettextback = File.ReadAllText(@"C:\temp\another_textfile.txt");
            File.WriteAllText(@"C:\temp\textfile.txt", gettextback);
        }
        catch
        {
            TextBox1.Visible = true;
            TextBox1.Text = "The file is not avaiable. Please contact your administrator!";
        }
    }

than:

I combine the methods:

    protected void GetWholeTextOfFile_Click(object sender, EventArgs e)
    {
        Method1();
        Method2();
        Method3();
        Method4();
    }
grekko
  • 53
  • 1
  • 11
  • If I implement the function "GetWholeTextOfFile_Click" it, shows me only the last. But I want that what every method put text in the TextBox. – grekko Jul 05 '12 at 08:02
  • You should edit your original post and put the code in there, not create a new post. – Polyfun Jul 10 '12 at 08:51