search specified string in files



  • Hi guys,

    i am trying to write an app. By which user will provied a word or string, the program will find out the files which coantains this word or string.

    For example there are three files: file1.cs, file2.cs, file3.cs
    the file1.cs contains the string "TtIsMe" .
    If a user type in the string "TtIsMe" and the file1.cs among the three files will be found out.

    Anyone can give me some tips how can i implement the search job.

    Thanks in advance



  • Hi,
    use DirectoryInfo::GetFiles to get an array of fileinfo objects. Then load every file content in a string and compare it with the search pattern:

    private String ReadFile(FileInfo nfo)
            {
    
                using (FileStream f = new FileStream(nfo.FullName, FileMode.Open, FileAccess.Read)) {
    
                    if (f == null) throw new ApplicationException(nfo.FullName + " kann nicht gelesen werden.");
    
                    using (StreamReader r = new StreamReader(f)) {
    
                        return r.ReadToEnd();
                    }
                }
            }
    


  • witte schrieb:

    Hi,
    use DirectoryInfo::GetFiles to get an array of fileinfo objects. Then load every file content in a string and compare it with the search pattern:

    👍 👍 👍 👍

    Thanks very much!!!!!!! it is really helpful!!!!!!!!!!!!!



  • Macht doch nicht immer die Aufgaben anderer Entwickler. Ein Tipp auf die Funktionen sollte IMHO reichen. willyyan fragt hier gerne nach Lösungen, die er vermutlich von seinem Auftraggeber gegen Geld lösen soll und sammelt sich das hier kostenlos ein.



  • Wirtschaft schrieb:

    Macht doch nicht immer die Aufgaben anderer Entwickler. Ein Tipp auf die Funktionen sollte IMHO reichen. willyyan fragt hier gerne nach Lösungen, die er vermutlich von seinem Auftraggeber gegen Geld lösen soll und sammelt sich das hier kostenlos ein.

    hoho......... 😃

    if u think in this way, i feel sorry.

    I think u never know, why thousands of software developers devoting hundreds of unpaid hours to create free programs(for example Linux).

    Sharing is alway the keyword.

    Yes, i prefer the 'Lössung',because i am newbie to C#, but any kinds of tips also would be appreciated. One day i would be also good at C# ,i will give out my help as well.

    I like here, because the guys here are really nice, they help me a lot!

    if u do not wanna give any help, it is ok. but u DO not have right to keep other from providing helps.



  • simon.gysi schrieb:
    http://www.google.ch/search?hl=de&q=c%23+regex&meta=
    http://www.gidf.de

    😞

    😕

    i hope u know what means interactive

    such kind of answer i have never got before. Really make me sad.

    Hope someone can give some useful tips.

    Das zeugte zumindest nicht davon, das Du bereit bist selbst mal in die Dokumentation zu schauen und fertige Lösungen erwartest. (Die Treffer waren sehr gut und lagen direkt zum lesen bereit.)

    Aber irgendwie wird von einigen wohl nur eine copy und paste fähige Lösung erwartet. Selbst in die Doku schauen scheint out zu sein. 🙄


Anmelden zum Antworten