-6

我想从一些文本文件中获取文本并将其插入到文本框中。我创建了一些方法来获取文件的文本。我的目标是将文本文件合并为一个文本。问题是程序只显示最后一个文本文件。

如何将所有文本文件合并为一个?

4

3 回答 3

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
于 2012-07-03T09:10:25.607 回答
1

在没有看到您的代码的情况下,这是我能做的最好的事情:

myTextBox.Text = 
  File.ReadAllText("file1") + 
  File.ReadAllText("file2") + 
  File.ReadAllText("file3");
于 2012-07-03T09:14:30.837 回答
0

我有以下方法:(它们远程相同)

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!";
        }
    }

比:

我结合了这些方法:

    protected void GetWholeTextOfFile_Click(object sender, EventArgs e)
    {
        Method1();
        Method2();
        Method3();
        Method4();
    }
于 2012-07-05T07:59:40.007 回答