0

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();






}
3
Contributors
8
Replies
32
Views
2 Years
Discussion Span
Last Post by techker
0

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

the last is to insert new info

0

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.

Edited by Lucaci Andrew

0

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..

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.