HelpSteamWriter Help

Posts 110 of 10 · Page 1 of 1
SteamWriter Help
Hello guys, recently while making a program my SteamWriter has been giving me trouble. Anyway my program has a function where it saves what is in listboxs and then imports the text when the form loads back into the listboxes. It also has a save button, everything works fine except for one thing; if I clear the listboxses the SteamWriter doesn't want to clear the textfiles with it and imports what was in the listbox before I cleared it, however it will keep on adding text fine. Any help C# community? This is the code for the SteamWriter and SteamReader:

SteamWriter:
Code:
 {
                var dir = @"C:\NormenJaydenFBI's Key Manager";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                using (StreamWriter writer = new StreamWriter("C:\\NormenJaydenFBI's Key Manager\\Clean.txt", true))
                {
                    {
                        string listbox1Text = "";
                        foreach (string li in listBox1.Items)
                        {
                            listbox1Text = listbox1Text + li + Environment.NewLine;
                        }
                        listbox1Text = listbox1Text + Environment.NewLine;
                        writer.Write(listbox1Text);
                    }
                }
                using (StreamWriter writer = new StreamWriter("C:\\NormenJaydenFBI's Key Manager\\All.txt", true))
                {
                    string listbox1Text = "";
                    foreach (string li in listBox5.Items)
                    {
                        listbox1Text = listbox1Text + li + Environment.NewLine;
                    }
                    listbox1Text = listbox1Text + Environment.NewLine;
                    writer.Write(listbox1Text);
                }
                using (StreamWriter writer = new StreamWriter("C:\\NormenJaydenFBI's Key Manager\\In-Use.txt", true))
                {
                    string listbox1Text = "";
                    foreach (string li in listBox2.Items)
                    {
                        listbox1Text = listbox1Text + li + Environment.NewLine;
                    }
                    listbox1Text = listbox1Text + Environment.NewLine;
                    writer.Write(listbox1Text);
                }
                using (StreamWriter writer = new StreamWriter("C:\\NormenJaydenFBI's Key Manager\\Sold.txt", true))
                {
                    string listbox1Text = "";
                    foreach (string li in listBox3.Items)
                    {
                        listbox1Text = listbox1Text + li + Environment.NewLine;
                    }
                    listbox1Text = listbox1Text + Environment.NewLine;
                    writer.Write(listbox1Text);
                }
                using (StreamWriter writer = new StreamWriter("C:\\NormenJaydenFBI's Key Manager\\Hacked On.txt", true))
                {
                    string listbox1Text = "";
                    foreach (string li in listBox4.Items)
                    {
                        listbox1Text = listbox1Text + li + Environment.NewLine;
                    }
                    listbox1Text = listbox1Text + Environment.NewLine;
                    writer.Write(listbox1Text);
                }
            }
        }
Steam Reader:
Code:
StreamReader stRead = new StreamReader("C:\\NormenJaydenFBI's Key Manager\\Clean.txt", true);
            {
                while (!stRead.EndOfStream)
                {
                    listBox1.Items.Add(stRead.ReadLine());
                }
            }
            stRead.Close();
            StreamReader stRead2 = new StreamReader("C:\\NormenJaydenFBI's Key Manager\\In-Use.txt", true);
            {
                while (!stRead2.EndOfStream)
                {
                    listBox2.Items.Add(stRead2.ReadLine());
                }
            }
            stRead2.Close();
            StreamReader stRead3 = new StreamReader("C:\\NormenJaydenFBI's Key Manager\\Sold.txt", true);
            {
                while (!stRead3.EndOfStream)
                {
                    listBox3.Items.Add(stRead3.ReadLine());
                }
            }
            stRead3.Close();
            StreamReader stRead4 = new StreamReader("C:\\NormenJaydenFBI's Key Manager\\Hacked On.txt", true);
            {
                while (!stRead4.EndOfStream)
                {
                    listBox4.Items.Add(stRead4.ReadLine());
                }
            }
            stRead4.Close();
            StreamReader stRead5 = new StreamReader("C:\\NormenJaydenFBI's Key Manager\\All.txt", true);
            {
                while (!stRead5.EndOfStream)
                {
                    listBox5.Items.Add(stRead5.ReadLine());
                }
            }
            stRead5.Close();
Code:
using(StreamWriter sr = new StreamWriter( ... ))
{
    if(listBox1.Items.Count > 0)
    {
             foreach(Object o in  listBox1.Items)
      {
            sr.WriteLine(o.ToString());
      }
      sr.Flush();
    }
}
Not sure if this's what you wanted tho
Why not just delete the txt file when clearing the listbox.
Quote Originally Posted by Pingo View Post
Why not just delete the txt file when clearing the listbox.
I fixed it now, but it wants to import blank lines into the listBox.
If you are reading your textfiles, you could something like that:

Code:
string fileLine = "";
StreamReader youStreamReader = new StreamReader("YourTextFile.txt", true);
{
     while ((fileLine = yourStreamReader.ReadLine()) != null)
     {
           if(fileLine != "")
           {
                 listBox.Items.Add(fileLine);
           }
     }
}

youStreamReader.Close();
youStreamReader.Dispose();
This will check if the line is empty or not.
If its empty then it wont add the file to your ListBox.

Hopefully i understand you right and that will help you a bit.

P.s.: Why you dont use the StreamWriter.WriteLine method on your stream writers?
I think its better and you dont need to add the new line thing manually
Sure, np.
Does your reading and writing stuff working now?
Quote Originally Posted by Devil589 View Post
Sure, np.
Does your reading and writing stuff working now?
Yes it does, perfectly.
Also another question about a different program. I have the program trying to give permissions full control to a group that has denied access; the coding doesn't seem to work as it crashes giving me the error registry access is not allowed, though I edited the manifest and ran the program as administrator.
Simplier version that doesn't require messing around with streams (by the way, it's best to wrap them in a using statement if you ever user them).

This method requires .NET 3.5 and above:
Code:
listBox1.Items.AddRange(File.ReadAllLines("blah.txt").Select(s => (object)s).ToArray());
Otherwise for .NET 2.0 and 3.0:
Code:
foreach(string line in File.ReadAllLines("blah.txt"))
{
    if(!string.IsNullOrEmpty(line))
        listBox1.Items.Add(line);
}
Also, writing them (requires .NET 3.5 and above since we're using LINQ):
Code:
File.WriteAllLines("blah.txt", listBox1.Items.Cast<object>().Select(o => o.ToString()));
Otherwise if you're using .NET 2.0 or 3.0:
Code:
var lines = new List<string>();
foreach(object o in listBox1.Items)
    lines.Add(o.ToString());
File.WriteAllLines("blah.txt", lines.ToArray());
Remember to add the System.Linq namespace if you use the LINQ methods.
Posts 110 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?