C# - Filling an array from a file...

Associate
Joined
18 Oct 2002
Posts
220
Location
London, UK
Hi All,

I'm slowly getting to grips with C#, but I've hit a problem with something...

I've written an application the reads in certain information from a config file.

Currently I keep all the information I need (hostnames) in 1 line, and comma delimit it. I then read the file with something like the below.

using (FileStream myFileStream = new FileStream(myFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (StreamReader myStreamReader = new StreamReader(myFileStream))
{
string data = myStreamReader.ReadLine();
string[] myArray = data.Split(new char[] { ',' });
return myArray;
}
}

which splits it at every comma, then fills the array, I then perform actions on each value in the array. My problem is that this gets messy when the line in the files gets bigger. I want to be able to have each entry on a new line.

Is there a way I can split on '\r\n' or something? Or is there a better way of doing it than this?

Cheers,
Melon.
 
Create a Class with variables for the "fields" in each line in your config file.

Create an ArrayList, and for each element create a class instance, then populate the class variables with data from your config file.

there are many ways though, this is just 1.
 
I would probably do it something like this:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace WindowsApplication1
{
    class MyItems
    {
        private string _item = "";

        public string Item {
            get { return _item; }
            set { _item = value; }
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ArrayList result = this.getList();

            foreach (MyItems getItems in result)
            {
                MessageBox.Show(getItems.Item);
            }
        }

        public System.Collections.ArrayList getList()
        {
            StreamReader sr = new StreamReader(@"C:\Test.txt");

            ArrayList tempArrList = new ArrayList();

            while (sr.EndOfStream == false)
            {
                MyItems newItems=new MyItems();

                newItems.Item=sr.ReadLine();

                tempArrList.Add(newItems);
            }

            return tempArrList;
        }
    }
}
I have done this in the form but you could easily move it into a class for future use etc...

Hope this helps,

TrUz
 
You can create your own ConfigSections and handle any types you want.

Also you can link to other config files by directing the appSettings so another source, at least in a web app anyway, which makes it useful for having config files for Dev, QA and live environments
 
Aren't cofiguration files for ASP.NET applications though? That's the impression i get from MSDN at least.

How do you use generic types/arrays/any other type with application settings?
 
Inquisitor said:
Aren't cofiguration files for ASP.NET applications though? That's the impression i get from MSDN at least.

How do you use generic types/arrays/any other type with application settings?


Any .NET application can have a configuration file even a window service. The name are slightly different asp.net has web.config and other applications have app.config but they are both the same thing. you can even get you applcation to encrypt them so no one can read the values :-)

Writing your own ConfigurationSectionHandler you can handle any types you want just code whatever you want.
 
Back
Top Bottom