111,097
社区成员




const int SaveCount = 2;
int _currentIndex = 0;
object _lock = new object();
void listBoxAddItem(object item)
{
this.listBox1.Items.Add(item);
if (this.listBox1.Items.Count - _currentIndex > SaveCount)
{
lock (_lock)
{
string path = tbx_Save.Text;
string filePath = string.Format("{0}\\{1}.txt", path, DateTime.Now.ToString("yyyy-MM-dd"));
using (System.IO.StreamWriter sw = new StreamWriter(filePath, true))
{
for (; _currentIndex < this.listBox1.Items.Count; _currentIndex++)
{
sw.WriteLine(item.ToString());
}
sw.Close();
}
}
}
}
都要用这个方法添加itemFolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == DialogResult.OK)
{
string foldPath = dialog.SelectedPath;
DirectoryInfo theFolder = new DirectoryInfo(foldPath);
//theFolder 包含文件路径
string path = dialog.SelectedPath;
string filePath = string.Format("{0}\\{1}.txt", path, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff"));
using (System.IO.StreamWriter sw = new StreamWriter(filePath))
{
foreach (var item in this.listBox1.Items)
{
sw.WriteLine(item.ToString());
}
sw.Close();
}
}