【问题标题】:How can i check if a text file contain more then one line of text inside?如何检查文本文件中是否包含多于一行的文本?
【发布时间】:2012-08-13 18:33:32
【问题描述】:

我有这个代码:

BeginInvoke(new Action(() => tempNamesAndTexts.ForEach(Item => textBox1.AppendText(DateTime.Now + "===> " + Item + Environment.NewLine))));
                foreach (string items in tempNamesAndTexts)
                {

                        Logger.Write(items);


                }

一旦我运行程序,它将执行:Logger.Write(items); 然后文本文件将如下所示:

丹妮你好 罗恩嗨 耶尔再见 乔希,大家都好

下次我运行程序时,它会将相同的字符串写入文本文件。 我想检查这个字符串是否已经存在于文本文件中,不要再写它们,否则写,所以结果是每次我运行程序时,它只会写入记录器(文本文件),如果有的话。

这是记录器文本文件的字符串变量:

full_path_log_file_name

这个变量里面有:

C:\\Users\\Chocolade\\AppData\\Local\\ChatrollLogger\\ChatrollLogger\\log\\logger.txt

这是完整的代码,直到这部分是 DoWork 在我运行程序时总是做的部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using DannyGeneral;

namespace ChatrollLogger
{
    public partial class Form1 : Form
    {
        string log_file_name = @"\logger.txt";
        string full_path_log_file_name;
        string path_log;
        bool result;
        List<string> namesAndTexts;
        WebResponse response;
        StreamReader reader;
        string profileName;
        string profileNameText;
        string url;
        string testingUrl;
        int index;
        List<string> names; 
        List<string> texts;
        WebClient wc;


        public Form1()
        {
            InitializeComponent();
            Logger.exist();
            wc = new WebClient();
            result = true;
            url = "http://chatroll.com/rotternet";
            testingUrl = "http://chatroll.com/testings";
            backgroundWorker1.RunWorkerAsync();
            path_log = Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\log";
            full_path_log_file_name = path_log + log_file_name;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
        {
            BackgroundWorker worker = sender as BackgroundWorker; 
            List<string> tempNamesAndTexts = new List<string>();
            string tempDownload = downloadContent();
            GetProfileNames(tempDownload);
            GetTextFromProfile(tempDownload);
            for (int i = 0; i < names.Count; i++)
            {
                tempNamesAndTexts.Add(names[i] + " " + texts[i]);

            }
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => tempNamesAndTexts.ForEach(Item => textBox1.AppendText(DateTime.Now + "===> " + Item + Environment.NewLine))));
                foreach (string items in tempNamesAndTexts)
                {

                        Logger.Write(items);
                        string t = full_path_log_file_name;

                }
            }

【问题讨论】:

    标签: c#


    【解决方案1】:

    这样的?

    string path = "test.txt";
    string valueToWrite = "....";
    
    //only write to the file, if the specified string is not already there
    if(!File.ReadLines(path).Any(l => string.Equals(l, valueToWrite)))
    {
        File.AppendAllText(path, valueToWrite);
    }
    

    【讨论】:

    • Dave Bish 我将我的代码更改为: if (!File.ReadLines(full_path_log_file_name).Any(l => string.Equals(l, items))) { File.AppendAllText(full_path_log_file_name, DateTime .Now + "===> " + items + Environment.NewLine);并且它现在再次无法正常工作,它在我运行我的应用程序时第二次添加文本。如果有任何其他文本,我希望它只添加新文本,并且任何其他文本都是相同的,不要添加到文本文件中。
    【解决方案2】:

    使用File.ReadAllLines

    string[] array = File.ReadAllLines("test.txt");
    if(array.Length > 0)
    {
    // lines exist
    }
    

    【讨论】:

    • @Oded,我没想过,你说的完全正确。在这种情况下,这不应该是这种方法。但帖子中没有指定文件大小为 1 GB
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多