选择文件并使用文件夹浏览对话框将其保存到定义的位置 [英] selecting files and saving it to the defined location using folder browse dialog

查看:91
本文介绍了选择文件并使用文件夹浏览对话框将其保存到定义的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将此代码提供给用户以选择文件夹,以便将我的两个文件复制到该文件夹​​.代码是这样的:

I had made this code for user to select a folder so that my two files will be copied to that. The code is this:

string sourcePath = @"C:\Documents and Settings\akib\";
string fileName1 = @"untitled.jpg";
string fileName2 = @"Copyuntitled.jpg";
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
    var destinationFolderName = folderBrowserDialog1.SelectedPath;
    if (Directory.Exists(destinationFolderName))
    {
        File.Copy(sourcePath + "/" + fileName1, destinationFolderName
                  + "/" + fileName1);
        File.Copy(sourcePath + "/" + fileName2, destinationFolderName 
                  + "/" + fileName2);
    }
}

但是现在我想扭转它.也就是说,如果用户在某个位置有两个文件,我要将其复制到c:\programfiles\myfolder.那么在这种情况下可以使用FolderBrowseDialog吗?如果是,怎么办?

But now I want to to reverse of it. That is if user have two files in some location I want to copy that to the c:\programfiles\myfolder. So FolderBrowseDialog can be used in such case? If yes how?

推荐答案

为此,您需要使用MultiSelect属性设置为true的OpenFileDialog类:

For that you would want to use the OpenFileDialog class with the Multiselect property set to true:

string destination = @"c:\programfiles\myfolder";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (DialogResult.OK == ofd.ShowDialog()) {
    foreach (string file in ofd.FileNames)  {
        File.Copy(file, Path.Combine(destination, Path.GetFileName(file)));
    }
}

这篇关于选择文件并使用文件夹浏览对话框将其保存到定义的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆