hi..who knows how to append the list box to the exist text file?

FileStream fs2 = new FileStream(@"C:\Users\Fish\Desktop\fyp-coding\Combine.txt", FileMode.Append, FileAccess.Write,FileShare.Write);//create text file    
            StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);

            string writetext = ListItemCom2.SelectedItems.ToString();
            sw2 = File.AppendText(@"C:\Users\Fish\Desktop\fyp-coding\Combine.txt");
            // write to the text file  
           
               sw2.Flush();
               sw2.Close();

this is my coding.But,it contain errors.Who can help me..?

Dani AI

Generated

The immediate problem in ’s code is treating the SelectedItems collection as a single string. SelectedItems is a collection of objects and must be enumerated; calling ToString() on the collection just returns the collection type name. was right to suggest iterating, and his final example shows a safe append pattern—but it iterates over all items rather than only the selected ones. A clean, robust approach is to build a sequence of display strings for the selected entries and append them in one operation (or open the writer once and write all lines), which avoids reopening the file repeatedly and ensures proper disposal of resources.

using System.IO;
using System.Linq;

var path = @"C:\path\to\output.txt";
var lines = listBox1.SelectedItems.Cast<object>().Select(i => listBox1.GetItemText(i));
File.AppendAllLines(path, lines, Encoding.UTF8);

This snippet collects the displayed text for each selected item (works whether items are strings or bound objects), then appends all lines at once. File.AppendAllLines creates the file if it does not exist and closes it when finished. If LINQ is not available, open a single StreamWriter inside a using block and write each selected item with WriteLine.

Checklist and common pitfalls:

  • Don’t call the file-open API inside a loop (that reopens the file per item). Open once or use AppendAllLines.
  • If the ListBox is data-bound to custom objects, prefer listBox.GetItemText(item) or cast to the object type and use the property you want to save.
  • Watch encoding and permissions (catch UnauthorizedAccessException/IOException).
  • For long I/O on the UI thread, run the write on a background task to keep the UI responsive.
  • Verify SelectedItems.Count before writing so empty selections don’t create blank entries.

Recommended Answers

All 5 Replies

string writetext = ListItemCom2.SelectedItems.ToString();

Is the error. You want to get an array and put it into a stirng. No go.

You needa loop, which will go through the listBox items, like:

FileStream fs2 = new FileStream(@"C:\Users\Fish\Desktop\fyp-coding\Combine.txt", FileMode.Append, FileAccess.Write, FileShare.Write);//create text file    
            StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);

            for (int i = 0; i < ListItemCom2.SelectedItems.Count; i++)
                sw2 = File.AppendText(@"C:\Users\Fish\Desktop\fyp-coding\Combine.txt");
            sw2.Flush();
            sw2.Close();

Hope it helps,
Mitja

Erm...I want to write my content of listbox to the exist text file...? it does not work...

What now, is it working or now? You marked the thread as answered, why? Your last post didnt sound like it has beed salved.

Or you have you found out the solution?

haven't solve yet..still finding the solution..sorry for confusing you.

This code will do what you want:

string path = @"C:\1\test21.txt";
            if (!File.Exists(path))
            {
                FileStream fs = File.Create(path);
                fs.Close();
            }

            using (TextWriter tw = File.AppendText(path))
            {
                foreach (string item in listBox1.Items)
                    tw.WriteLine(item);
            }

It creates a file if it does not exist yet, and if it does exist, it only appends the text into a new line.
Its tested and it works 100%.
Hope it helps,
Mitja

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.