PDA

View Full Version : جستجو در فايل



bghad1
دوشنبه 07 دی 1388, 01:15 صبح
با سلام...
اميدوارم كه حالتون خوب باشه...
مي خواستم ببينم اگه من بخوام مثلاً با فايلها يه چيزي رو بگيرم و ذخيره كنم مثلاً توي يه فايل با هر پسوندي، بعد بخوام حالا از طريق برنامم، يه كلمه مورد نظرم تو اون فايل رو مشخص كنم كه تكراريه يا نه..
بايد چيكار كنم؟

ahrimaneahurai
چهارشنبه 09 دی 1388, 19:54 عصر
منظورت مثل گوگل دسکتاپ هست دیگه ؟

debugger
پنج شنبه 10 دی 1388, 09:12 صبح
طریقه جستجوی یک کلمه در داخل فایل با استفاده از Regular Expressions

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace searchTxtFile
{
class searchTxt
{
static void Main()
{
string fName = @" ";//path to text file
StreamReader testTxt = new StreamReader(fName);
string allRead = testTxt.ReadToEnd();//Reads the whole text file to the end
testTxt.Close(); //Closes the text file after it is fully read.
string regMatch = " ";//string to search for inside of text file. It is case sensitive.
if (Regex.IsMatch(allRead,regMatch))//If the match is found in allRead
{
Console.WriteLine("found\n");
}
else
{
Console.WriteLine("not found\n");
}
}
}
}