如何保存ListBox中数据 在指定路径下

qaqaqaaaaaa 2020-03-06 09:09:28
C# 如何将ListBox中的数据保存进我TextBox控件指定的路径中? 如下图

如下 是我写的Button获取文件路径 显示在TextBox中 我该如何去保存呢
private void btn_Save_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";

if (dialog.ShowDialog() == DialogResult.OK)
{
string foldPath = dialog.SelectedPath;
DirectoryInfo theFolder = new DirectoryInfo(foldPath);

//theFolder 包含文件路径
tbx_Save.Text = dialog.SelectedPath;
FileInfo[] dirInfo = theFolder.GetFiles();
//遍历文件夹
foreach (FileInfo file in dirInfo)
{
MessageBox.Show(file.ToString());
}
}
}
...全文
459 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bridge_go 2020-03-09
  • 打赏
  • 举报
回复
就是往 ListBox 添加 item 时都要用这个方法 每添加 SaveCount个数据就将未写入的item写入到txt
qaqaqaaaaaa 2020-03-09
  • 打赏
  • 举报
回复
我尝试了一下 还是 有很多问题 不太能理解你的代码 还是谢谢你 非常感谢 先结帖
qaqaqaaaaaa 2020-03-09
  • 打赏
  • 举报
回复
foreach (var data in this.listBox1.Items)
{
if (!this.listBox2.Items.Contains(data))
{
this.listBox2.Items.Add(data);
}
label3.Text = (listBox2.Items.Count).ToString();
}
listBox1.Items.Clear();
if (label3.Text != comboBox1.Text)
{

MessageBox.Show("数据有误!", "错误");
}
else
{
}
可是我之前就将数据添加进ListBox中并且去重了
qaqaqaaaaaa 2020-03-06
  • 打赏
  • 举报
回复

追问一条 为什么 我在选择路径的时候 去新建文件夹 就会报错 第二次新建就不会出问题
Bridge_go 2020-03-06
  • 打赏
  • 举报
回复
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();
            }
        }
    }
}
都要用这个方法添加item
qaqaqaaaaaa 2020-03-06
  • 打赏
  • 举报
回复
引用 2 楼 Bridge_go 的回复:
FolderBrowserDialog 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();
}
}
我刚试了你的代码 发现我描述有误 我的目的是 写一个函数 我可以手动选择保存路径 然后 当ListBox中的数据量达到一定数量我就调用函数 将数据保存在我选择的路径下 可以用任何格式保存 我的代码只实现了点击选择路径后 将路径显示在TBX控件中 请教一下 怎么去写一个保存函数
Bridge_go 2020-03-06
  • 打赏
  • 举报
回复
FolderBrowserDialog 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();
    }
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧