Click here to Skip to main content
14,603,917 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Sir
I am uday,

I am developing application c sharp win form. and I want to read textFile.

and store in string array format.
please give solution.

How to read text file and store it into string array.and my text file contains arount 20000 rows.
Posted
Comments
Pravin Patil, Mumbai 24-Aug-11 7:25am
   
First of all let me tell you that it is not a good option to store a whole 20,000 line file in an array.
Array refers to e.g array[10]
Do you want to store first line at array[0], second line at array[1] etc....?
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
udusat13 24-Aug-11 7:24am
   
thanks sir.
please tell me in details and i want count also
Rob Philpott 24-Aug-11 7:26am
   
Very nice solution, much better than mine below. You see, you learn something new everyday.
Prerak Patel 24-Aug-11 7:33am
   
There is sample code in link itself. Did you check the link?
Simon Bang Terkildsen 24-Aug-11 8:41am
   
Nice and simply +5
Rate this:
Please Sign up or sign in to vote.

Solution 2

Use a StreamReader to read the file a line at a time, then add each row to a List<string>. Then use .ToArray() on that.
   
v3
Comments
udusat13 24-Aug-11 7:27am
   
please sir give me example
Rate this:
Please Sign up or sign in to vote.

Solution 3

first make one button and textbox and try this code.

private void button1_Click(object sender, EventArgs e)
{

string fpath="E:\\one.txt";
StreamReader dr = new StreamReader(fpath);
string[] arr=new string[1000];
int i = 0;
string st = "";
while (dr.Peek() !=-1)
{
arr[i] = dr.ReadLine();
st = st + arr[i].ToString()+"\n";
i++;
}
textBox1.Text = st;
}

here i declared string array and that size is 1000, and get value from reader.

i hope this is useful to you;
take care bye bye
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100