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

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.