سلام دوستان ! در این تاپیک جستجو در محتوای فایل تکست در سی شارپ C#.NET رو براتون آماده کردم.
کد جستجو در محتوای فایل تکست در سی شارپ C#.NET :
static void Main(string[] args) { Console.WriteLine(" masire file ra vared konid : "); var filePath = Console.ReadLine(); Console.WriteLine(" matni ke mikhahid search shavad ra vared konid : "); var searchTerm = Console.ReadLine(); var fsLib = new FSLibrary(); Console.WriteLine("No. of matches: {0}", fsLib.SearchOccurances(filePath, searchTerm)); Console.ReadKey(); } public int SearchOccurances(string filePath, string searchTerm) { int occurances = 0; var fileContents = File.ReadAllText(filePath); if (!String.IsNullOrEmpty(fileContents)) { MatchCollection matches = Regex.Matches(fileContents, searchTerm, RegexOptions.IgnoreCase); if (matches != null) { occurances = matches.Count; } } return (occurances); }
موفق و پیروز باشید
یا علی