0

我有这个代码:

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;

                }
            }
4

2 回答 2

1

使用File.ReadAllLines

string[] array = File.ReadAllLines("test.txt");
if(array.Length > 0)
{
// lines exist
}
于 2012-08-13T10:16:18.530 回答
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);
}
于 2012-08-13T10:33:32.257 回答