Click here to Skip to main content
15,885,895 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 7: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
 
Share this answer
 
Comments
Maciej Los 4-Jan-15 16:09pm    
+5Please, see my answer.
Sergey Alexandrovich Kryukov 5-Jan-15 0:23am    
Thank you, Maciej.
—SA
you can read file text into string as below
C#
string readText = File.ReadAllText(path);

then you can search within the string [^]
 
Share this answer
 
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.
Sergey Alexandrovich Kryukov 4-Jan-15 14:30pm    
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.
 
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