我有这样的代码:如何检查文本文件中是否包含多行文本?
BeginInvoke(new Action(() => tempNamesAndTexts.ForEach(Item => textBox1.AppendText(DateTime.Now + "===> " + Item + Environment.NewLine))));
foreach (string items in tempNamesAndTexts)
{
Logger.Write(items);
}
一旦IM运行程序会做:Logger.Write(项目); 然后将文本文件看起来像:
丹尼尔你好 罗恩喜 耶尔再见 乔希大家好
下一次运行程序会写入文本的即时消息相同的字符串。 我想检查一下,如果这个字符串已经存在于文本文件中,不要再写入它们,否则写入所以结果会是每次运行程序时它都会向记录器(文本文件)写入新的字符串,如果有的话。
这是记录文本文件的字符串变量:
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;
}
}
Dave Bish我将我的代码更改为:if(!File.ReadLines(full_path_log_file_name).Any(l => string.Equals(l,项目))) File.AppendAllText(full_path_log_file_name,DateTime.Now +“===>”+ items + Environment.NewLine); }并且它现在不能再次正常工作了,现在它再次在我的应用程序运行时再次添加文本。我希望它只添加新的文本,如果有任何和任何其他文本相同的不添加到文本文件。 – user1593871 2012-08-13 15:03:54