我有這樣的代碼:如何檢查文本文件中是否包含多行文本?
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