Click here to Skip to main content
14,733,599 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I want to find string in text file(e.g C:\send.txt) in my windows form.
but I don't know what is code of this?

thanks for help.

Edit OP comment from solution:-
sorry
my pattern for save text in my file is :
154879,1234567895148726935
159874,5649872315487623148
154879,3025421056897461250
.
.
.


it is two section.

OP also states file is 3000+ lines
Posted
Updated 4-Jan-15 8:25am
v2
Comments
Maciej Los 4-Jan-15 16:04pm
   
Have you tried anything?

It depends on the size of the file. If the files are small enough, you can use Solution 2. If the files are too big for that, you would need to read line by line and do the search in each line. You can choose to read the bigger chunk of lines and perform the search in the chunk.

More importantly, you can improve representation of data. As those strings represent numbers, you can store binary numbers, which would be much faster. Also, you could greatly accelerate search by having your lines (or numbers) sorted. The lines are of the same size, so you could have index file (or data in memory) indexing location of each line (or number, or pair of numbers). You could dramatically accelerate the search if you cannot sort the file itself, but sort the data in index file according your priority. Of course, it all worth doing if your files are big enough and throughput is important.

—SA
   
Comments
Maciej Los 4-Jan-15 16:09pm
   
+5 Please, see my answer.
   
Thank you, Maciej.
—SA
you can read file text into string as below
string readText = File.ReadAllText(path);

then you can search within the string [^]
   
Comments
CHill60 4-Jan-15 13:26pm
   
OP trying to contact you by posting another solution (removed)... quote...
thanks DamithSL.
but my file's size is big about 3000 lines!
this solution is not Optimized.
   
I voted 4. You considered only one case, when the files are small enough for this. Also, the form of data presentation could be questioned. Please see my answer (where your solution is credited).
—SA
The simplest way to import data from comma delimited file is to use ADO.NET[^]. And of course, as Sergey mentioned, it depends on many factors.

See my past answers:
How to read from csv file using c#[^]
How change the Delimited character when using MS.ACE.OLEDB to read Text file[^]

Try to do something, then come back here and ask detailed question.
   

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