Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have lots of .txt file in a folder and all txt file contains some information. i have two text box 1st text box use for path of that folder where txt file present i put only folder path not txt path.In second text box type a word like Rose and click on submit button then how i find in which txt file contains that word "Rose" and how i store that txt file name in a variable.


i write code like this...

C#
string Path = Convert.ToString(txtBrowse.Text.Trim());
            string Word = Convert.ToString(txtWord.Text.Trim());

          
            foreach (string file in Directory.GetFiles(Path, "*.txt"))
            {
                contents = File.ReadAllText(file);

                if (contents.Contains(Word))
                {
                    string foldername = file;

                    lbltab.Text = foldername;

                    break;
                }
            }


it gives me full path but i want only txt file name.


please help me solve it...
thanks...
Posted
Updated 4-May-14 19:37pm
v2

Make loop for all text files in a folder and read each file using reader and compare your data(Ex:Rose) with read data.
 
Share this answer
 
what you need is Path.GetFileName Method[^]

C#
string fileName = @"C:\mydir\myfile.ext";
string result = Path.GetFileName(fileName);//returns 'myfile.ext' 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900