写入文本文件并读取该文本文件

时间:2010-12-01 06:10:02

标签: c#

我已使用此代码

 var dest1 = File.AppendText(Path.Combine(_logFolderPath, "log1.txt"));
   dest1.WriteLine(line.Trim());

写入文本文件log1.txt之后我必须阅读此文本文件...

我已在变量中声明......我知道这是不可能的..但我不知道如何

using (var file = File.OpenText(dest1))

如何打开该文本文件并使用

读取该文件
while ((line2 = file.ReadLine()) != null)

有什么建议吗?

编辑:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("select codesnippet from edk_custombrsnippet_vw", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            string line = dt.Rows[0].ItemArray[0].ToString().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine);
            ;

            //MessageBox.Show(line);

            string Filepath2 = TextBox1.Text;
            int counter = 1;
            string line2;

            if (File.Exists(Filepath2) )
            {
                DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
                var _logFolderPath = Path.Combine(textboxPath.Text.Trim(), "log");
                if (Folder.Exists)

                    if (!Directory.Exists(_logFolderPath))
                        Directory.CreateDirectory(_logFolderPath);

                string filename = Path.Combine(_logFolderPath, "log1.txt");
                var dest1 = File.AppendText(filename);

                    dest1.WriteLine(line.Trim());

                using (var file = File.OpenText(filename))
                {
                    using (var file2 = File.OpenText(Filepath2))
                    {
                        bool time = false;

                        while ((line2 = file2.ReadLine()) != null)
                        {
                            using (var dest = File.AppendText(Path.Combine(_logFolderPath, "log.txt")))
                            {
                                if (!time)
                                {
                                    dest.WriteLine("");
                                    dest.WriteLine("---------------------" + DateTime.Now + "---------------------");
                                    time = true;
                                }
                                bool patternwritten = false;
                                while ((line = file.ReadLine()) != null)
                                {
                                    if (line.IndexOf(line2, StringComparison.CurrentCultureIgnoreCase) != -1)
                                    {
                                        if (!patternwritten)
                                        {
                                            dest.WriteLine("");
                                            dest.WriteLine("Pattern Name : " + line2);
                                            patternwritten = true;
                                        }
                                        dest.WriteLine("LineNo : " + counter.ToString() + " : " + "           " + line.Trim());
                                    }
                                    counter++;
                                }
                                //FilePath.BaseStream.Seek(0, SeekOrigin.Begin);
                                counter = 1;
                            }
                        }
                    }
                }

2 个答案:

答案 0 :(得分:4)

将文件名存储在如下变量中:

string filename = Path.Combine(_logFolderPath, "log1.txt");

然后在以下行中使用它:

var dest1 = File.AppendText(filename);
...
using (var file = File.OpenText(filename))

其余的应该按预期工作。在上面的代码中,您试图将StreamWriter传递给File.OpenText方法,这是错误的。

答案 1 :(得分:2)

写入后你没有关闭文件,所以你的程序仍然有一个打开的文件句柄,这意味着你无法打开另一个句柄来读取它。

这一位:

var dest1 = File.AppendText(filename);

dest1.WriteLine(line.Trim());

应该是:

using (var dest1 = File.AppendText(filename))
{
    dest1.WriteLine(line.Trim());
}