OpenFileDialog从文本框中多重选择目标路径

时间:2016-04-28 11:44:59

标签: c# multi-select openfiledialog

每个人,

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

请在下面找到我的代码:

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));
        } 
    }
}

任何意见都会受到赞赏!

2 个答案:

答案 0 :(得分:0)

这些东西是等价的。

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

我会删除Path.Combine调用的第一个,因为它对于分隔符来说是可移植且强大的。

答案 1 :(得分:0)

试试这个:

 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);
      }

缺少反斜杠

  
      
  • @" \"
  •