winforms - c# Search text files for specific string and get file path -
using c# in windows form need search directory "c:\xml\outbound"
file contains order number 3860457
, return path file contains order number can open file , display contents user in ricktextbox.
the end user have order number not know file contains order number why need search files till finds filecontaining order number , return path (e.g. "c:\xml\outbound\some_file_name_123.txt"
)
i new c# not sure start this. direction this?
sorry order number inside file need search each file contents order number , once file containing order number found return path file. order number not part of file name.
straight answer:
public string getfilename(string search){ list<string> paths = directory.getfiles(@"c:\xml\outbond","*.txt",searchoption.alldirectories).tolist(); string path = paths.firstordefault(p=>file.readalllines(p).any(line=>line.indexof(search)>=0)); return path; }
not-so straight answer:
even though above function give path given string (some handling of errors , edge cases may nice) terribly slow, if have lots of files. if that's case need tell more environment because chances you're doing wrong (:
Comments
Post a Comment