hi,
i have some values in a notepad (.dat file). i want to store the values one by one in an variable. i dont know how to read the file and store the vaues.
can u help plz...

Hi Sidd. welcome here!
Try this:

using (StreamReader sr = new StreamReader ("myfile.dat"))
{
    string s = sr.ReadLine();
//etc.
}
List<string> mylines = new List<string>();
            using (StreamReader sr = new StreamReader("myfile.dat"))
            {
                while (sr.ReadLine() != null)
                {                
                   mylines.Add(sr.ReadLine());
                }     
            }

Yeah, That should work. assuming each line is a separate value, otherwise you will need to load it all to memory as a string and split it to an array using the delimiter. Post back if you need more help.

Just in case, here is a method to read from a CSV or delimiter. It returns data to its caller.

static void Main(string[] args)
        {

            List<string> columns;
            List<Dictionary<string, string>> myData = GetData(out columns);

            foreach (string column in columns)
            {
                Console.Write("{0,-20}", column);
            }
            Console.WriteLine();

            foreach (Dictionary<string, string> row in myData)
            {                
                foreach (string column in columns)
                {
                    Console.Write("{0, -20}", row[column]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();          

                     
        }
        public static List<Dictionary<string, string>> GetData(out List<string> columns)
        {
            string strLine;
            string[] strArray;
            char[] charArray = new char[] { ',' };
            List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
            columns = new List<string>();


            try
            {               

                StreamReader sr = new StreamReader(new FileStream("C:\\TECHDATA.txt", FileMode.Open));

                strLine = sr.ReadLine();
                strArray = strLine.Split(charArray);

                for (int x = 0; x <= strArray.GetUpperBound(0); x++)
                {
                    columns.Add(strArray[x]);
                }

                strLine = sr.ReadLine();

                while (strLine != null)
                {
                    strArray = strLine.Split(charArray);
                    Dictionary<string, string> dataRow = new Dictionary<string, string>();

                    for (int x = 0; x <= strArray.GetUpperBound(0); x++)
                    {
                        dataRow.Add(columns[x], strArray[x]);
                    }
                    data.Add(dataRow);
                    strLine = sr.ReadLine();
                }
                sr.Dispose();
                return data;
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return data;
            
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.