بفرما دوست عزیز
string log = @"C:\username.txt";
using (System.IO.FileStream fs = new System.IO.FileStream(log, FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string line in listBox1.Items)
sw.Write(line + sw.NewLine,true);
}
MessageBox.Show("یوزنیم و پسورد ساخته شده با موفقیت ذخیره شد", "ذخیره اطلاعات");
}
}
البته اینجوری بنویسی بهتره
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
try
{
string[] s = {"asd", "sadd", "dwedfewfdwe", "Edfwef","fddsjkhkskdfhkjdhjkhkjhdkhk","eeeeeeeeee eeeeeeee"};
foreach (string s1 in s)
{
listBox1.Items.Add(s1);
}
string log = @"C:\username.txt";
if (File.Exists(log))
{
using (System.IO.FileStream fs = new System.IO.FileStream(log, FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string line in listBox1.Items)
sw.Write(line + sw.NewLine, true);
}
MessageBox.Show("یوزنیم و پسورد ساخته شده با موفقیت ذخیره شد", "ذخیره اطلاعات");
}
}
else
{
using (System.IO.FileStream fs = new System.IO.FileStream(log, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string line in listBox1.Items)
sw.Write(line + sw.NewLine, true);
}
MessageBox.Show("یوزنیم و پسورد ساخته شده با موفقیت ذخیره شد", "ذخیره اطلاعات");
}
}
}
catch (Exception ex)
{
MessageBox.Show("بروز خطا.لطفا دوباره تلاش کنید", "بروز خطا");
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("بروز خطا.لطفا دوباره تلاش کنید", "بروز خطا");
}