简体   繁体   English

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

[英]writing to a text file and reading that text file

I have used this code 我已经使用了这段代码

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

to write to a text file log1.txt after that i have to read this text file... 写一个文本文件log1.txt之后,我必须阅读这个文本文件...

i have declared in a variable... I know this is not possible..but i dont know how 我已经在变量中声明了...我知道这是不可能的..但是我不知道如何

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

How to open that text file and read that file by using 如何使用打开文本文件并读取该文件

while ((line2 = file.ReadLine()) != null)

Any suggestion?? 有什么建议吗?

EDIT: 编辑:

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

Store the filename in a variable like so: 将文件名存储在变量中,如下所示:

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

Then use it in the following lines: 然后在以下几行中使用它:

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

Then the rest should work as expected. 然后其余的应该按预期工作。 In your code above you were trying to pass a StreamWriter to the File.OpenText method which is wrong. 在上面的代码中,您试图将StreamWriter传递给File.OpenText方法,这是错误的。

You're not closing the file after writing to it, so your program still has an open handle to the file, which means that you can't open another handle to read from it. 写入文件后不会关闭文件,因此程序仍然具有文件的打开句柄,这意味着您无法打开另一个句柄来读取文件。

This bit: 这一点:

var dest1 = File.AppendText(filename);

dest1.WriteLine(line.Trim());

should be: 应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM