Hey guys is it posssible to read from file and add to collectionBase?For my litle project i need to use collection and read file but the app needs to save.

it saves to file but i find it faster and easyer to read from collection and search,edit,delete...

string[] lineOfContents = File.ReadAllLines("Myfile.txt");
foreach (var line in lineOfContents)
{
   string[] tokens = line.Split(',');
   comboBox1.Items.Add(tokens[0]);



   string lines = "THIS IS WERE I BLOCK";

            System.IO.StreamWriter file = new System.IO.StreamWriter("Conservateur.txt");
            file.WriteLine(lines);
            richTextBox1.AppendText(lines);
            file.Close();






}

Recommended Answers

All 8 Replies

System.IO.StreamWriter will never read anything from a file I guess ...

i read it from the begining ReadAllLines("Myfile.txt");

the last is to insert new info

It's kinda hard to tell what you're trying to accomplish here.
Putting

System.IO.StreamWriter file = new System.IO.StreamWriter("Conservateur.txt");
file.WriteLine(lines);
file.Close();

inside your foreach statement will do you no good, because it will overwrite the same file, over and over again.

var inMemoryProcessedItems = new List<string>();
using (var sw = new StreamWriter("yourfile.txt"))
{
    foreach (var line in lineContents)
    {
        // do stuff
        // process line
        string[] tokens = line.Split(',');
        if (tokens.Length > 1)
        {
            inMemoryProcessedItems.Add(tokens[0]);
            sw.WriteLine("Part 1: {0}. Part 2: {1}", tokens[0], tokens[1]);
        }
    }
}

This will save only after you've done your stuff inside the foreach (such as processing your information).

But again, you'll have to be a little bit more specific to what you're trying to accomplish.

but i find it faster and easyer to read from collection and search,edit,delete...

Indeed, in-memory objects can be accessed faster than those saved on disk, but depending on your project and your requirements, you'll need to see how much you're willing to load up into memory.

thx for the reply!

i got it going...but im stuck on serching the collection from a string?

like when the client inserts nip i need to search the collection and find the Nip corresponding to the clients info and show the info in a richtextbox..

This can be easily done with Linq(from .Net framework 3.5+). Create a query with the Where clause. Something like:

var list = new List<string>()
    { "string1", "string2", "string3", "asdf", "stringasdf" };
var search = "asdf";

var totalMatch = list.Where(i => i == search).ToList();
var partialMatch = list.Where(i => i.Contains(search)).ToList();

Console.WriteLine(totalMatch.Count);    // asdf
Console.WriteLine(partialMatch.Count);  // asdf and stringasdf

Take a look at all the Linq operators: Click Here.

So with this i can search CollectionBase?

Have a look at the CollectionBase reference from MSDN (Click Here).

Try it with your environment (hint: CollectionBase implements IList,
ICollection and IEnumerable).

thx for the help

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.