TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sin Yee
NA
145
74.7k
How to append the content in the list box to the text file...?
Mar 21 2011 1:03 PM
Hi..i don't know how to append the content of listbox to the exist text file? when i append the content of the listbox to the textfile, the previous data that i clear and not show in the list box will append together with the content of listbox (the only data that i want append to the text file) to the text file. What should i do to not append the previous data that i clear already.
I will upload the coding here.
private void Browse2_Click(object sender, EventArgs e)
{
using (OpenFileDialog fileOpen = new OpenFileDialog())
{
try
{
fileOpen.Filter = "All files(*.*)|*.*";
fileOpen.InitialDirectory = ".\\";
fileOpen.Title = "Open";
if (fileOpen.ShowDialog() == DialogResult.OK)
{
StreamReader sr2 = new StreamReader(fileOpen.FileName, Encoding.Default);
str2 = sr2.ReadToEnd();// read to all the contents of data2
sr2.Close();
ContentBox2.Text = str2;
file2Path.Text = fileOpen.FileName;
}
}
catch (Exception o)
{
string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (File.Exists(fileOpen.FileName))
{
StreamReader file2 = null;
try
{
file2 = new StreamReader(fileOpen.FileName);
while ((line2 = file2.ReadLine()) != null)
{
SplitItemList1 = "";
//sb2.AppendLine(line2);
//txtCaseInputs.Text = sb2.ToString().Trim();
line2 = line2.Replace("\r\n"," ");
String[] Split2 = (line2).Split(' ');//split
foreach (string byline in Split2)
{
SplitItemList1 += "\r\n" + byline;
}
//sb3.Append(SplitItemList1);
//MessageBox.Show(sb3.ToString().Trim());
//MessageBox.Show(SplitItemList1.Trim());
txtCaseInputs.Text = SplitItemList1.ToString().Trim();
GenCombItems2();
}
file2.Close();
// MessageBox.Show(sb2.ToString().Trim());
}
catch (Exception o)
{
string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
}
private void GenCombItems2()
{
lbCombinations2.Items.Clear();
int k = 2;
int n = txtCaseInputs.Lines.Length;
// txtNumCombinations.Text = Combination.Choose(n, k).ToString();
Combination c = new Combination(n, k);
string[] result = new string[k];
while (c != null)
{
result = c.ApplyTo(txtCaseInputs.Lines);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; ++i)
{
sb.AppendFormat("{0}{1}", result[i], " ");
}
c = c.Successor();
lbCombinations2.Items.Add(sb.ToString().Trim());
}
//txtCaseInputs.Clear();
DisplayResult2();
//MessageBox.Show(ShowResult2.ToString());
}
//String ShowResult2 = "\n";
private void DisplayResult2()
{
foreach (string addItems in lbCombinations2.Items)
{
//MessageBox.Show(sort);
if (!(list1.ContainsKey(addItems)))
{
list1.Add(addItems, 1);
}
else
{
int Count = (int)list1[addItems];
list1[addItems] = Count + 1;
//MessageBox.Show(list1[addItems].ToString());
}
// MessageBox.Show(list1[addItems].ToString());
}
IDictionaryEnumerator enumerator = list1.GetEnumerator();
ListItemCom2.Items.Clear();
while (enumerator.MoveNext())
{
ListItemCom2.Items.Add(enumerator.Key.ToString() + ":" + enumerator.Value.ToString() + "\n");
//ShowResult2 += enumerator.Key.ToString() + ":" + enumerator.Value.ToString() + "\n";
}
// MessageBox.Show(ShowResult2);
FileStream fs2 = new FileStream(@"C:\Users\Fish\Desktop\fyp-coding\Combine1.txt", FileMode.Append, FileAccess.Write, FileShare.Write);//create text file
StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);
foreach (string item2 in ListItemCom2.Items)
{
//sw2.WriteLine(item2.ToString());// write to the text file
}
sw2.Flush();
sw2.Close();
fs2.Close();
//SaveToTextFile2();
}
Reply
Answers (
2
)
Create Login Form on C#
SQL Database to DataGridView in C#.Net