写入文本文件

时间:2010-11-22 12:13:10

标签: c#

我有一个包含这些内容的文本文件

balamurugan,rajendran,chendurpandian
christopher
updateba

我已阅读这些文件并搜索了关键字ba和  我尝试写入另一个文本文件log.txt,但在执行我的代码后  我只是将第三行作为

`LineNo : 2 : updateba` 

我需要获得这两行

LineNo : 0 : balamurugan,rajendran,chendurpandian
LineNo : 2 : updateba

我正在使用此代码写入文本文件

if (File.Exists(FilePath))
        {
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(FilePath);
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(regMatch))
                {
                    DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
                    if (Folder.Exists)
                    {
                        var dir = @"D:\New folder\log";
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        File.WriteAllText(Path.Combine(dir, "log.txt"), "LineNo : " + counter.ToString() + " : " + line + "<br />");

                    }
                    else
                    {
                        Response.Write("<script language='javascript'>window.alert('Folder not found');</script>");
                    }
                    Response.Write("<script language='javascript'>window.alert('Pattern found');</script>");
                    Response.Write("LineNo : " + counter.ToString()+ " : " + line + "<br />");
                }

                else
                {
                    Response.Write("<script language='javascript'>window.alert('Pattern not found');</script>");
                }
                counter++;


            }
               file.Close();
        }
        else
        {
            Response.Write("<script language='javascript'>window.alert('File not found');</script>");
        }

我已使用此示例link text

任何建议???

3 个答案:

答案 0 :(得分:6)

您正在呼叫WriteAllText - 此会覆盖该文件;也许你应该File.AppendAllText?或者,更有效率,首先使用StreamWriter - 即

using (var dest = File.CreateText(path))
{
    while (loopCondition)
    {
        // snip
        dest.WriteLine(nextLineToWrite);
    }
}

将问题中的代码缩减为之类的最小密钥代码,例如:

DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
var dir = @"D:\New folder\log";
if (Folder.Exists)
{                
    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
}

if (File.Exists(FilePath))
{
    // Read the file and display it line by line.
    using (var file = File.OpenText(FilePath))
    using (var dest = File.AppendText(Path.Combine(dir, "log.txt")))
    {
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(regMatch))
            {
                dest.WriteLine("LineNo : " + counter.ToString() + " : " +
                     line + "<br />");
            }
            counter++;
        }
    }
}

答案 1 :(得分:1)

File.WriteAllText

创建新文件,将内容写入文件,然后关闭文件。 如果目标文件已存在,则会被覆盖。

源。 http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx

你可能想要创建一个缓冲区并在完成后将缓冲区写入文件。

  

编辑该死的20秒太晚了。

答案 2 :(得分:0)

你需要AppendAllText