【问题标题】:OpenFileDialog multiselect targetpath from textboxOpenFileDialog 从文本框中多选目标路径
【发布时间】:2016-08-23 03:49:10
【问题描述】:

大家好,

我想使用 openfiledialog 将多个选定的文件复制到定义为 @"C:\TestFolder\"+ textBox1.Text 的文件夹中。我的问题是程序以某种方式也在文件名中写入了文本框内容。

请在下面找到我的代码:

private void button3_Click(object sender, EventArgs e)
{
    OpenFileDialog od = new OpenFileDialog();
    od.Filter = "All files (*.*)|*.*";
    od.Multiselect = true;
    if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        string targetPath = @"C:\TestFolder\"+ textBox1.Text;
        string path = System.IO.Path.Combine(targetPath, textBox1.Text);

        if (!System.IO.Directory.Exists(targetPath)
        {
            System.IO.Directory.CreateDirectory(targetPath);
        } 
        foreach (string fileName in od.FileNames)
        {            
            System.IO.File.Copy(fileName, path + System.IO.Path.GetFileName(fileName));
        } 
    }
}

任何意见将不胜感激!

【问题讨论】:

    标签: c# multi-select openfiledialog


    【解决方案1】:

    这些东西是等价的。

    string targetPath = @"C:\TestFolder\"+ textBox1.Text;
    string path = System.IO.Path.Combine(targetPath, textBox1.Text);
    

    我会放弃Path.Combine 调用的第一个,因为它在分隔符方面是可移植且健壮的。

    【讨论】:

    • 感谢您的评论。我不知道为什么,但是如果我删除第一行或第二行,我的代码就会停止正常工作。它仅将文件复制到主文件夹 C:\TestFolder\ 中,而不是创建一个名称写在文本框中的空文件夹。
    • textBox1 是干什么用的?
    • 它是文件的 ID 号。我需要它用于文件结构。
    【解决方案2】:

    试试这个:

     string Main_dir = @"C:\TestFolder\";
     string Sub_dir = textBox1.Text + @"\";
     string targetPath = System.IO.Path.Combine(Main_dir, Sub_dir);
          {
               if (!System.IO.Directory.Exists(targetPath))
          {
               System.IO.Directory.CreateDirectory(targetPath);
          }
               foreach (string fileName in od.FileNames)
    
               System.IO.File.Copy(fileName, targetPath + System.IO.Path.GetFileName(fileName), true);
          }
    

    缺少反斜杠

    • @"\"

    【讨论】:

      猜你喜欢
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多