使用打开的文件对话框将两个文件从一个位置保存到另一个 [英] saving two files from one location to another using open file dialog

查看:87
本文介绍了使用打开的文件对话框将两个文件从一个位置保存到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用winform c#并且我被困在打开文件对话框中,因为我正在处理它第一次我希望将两个文件从一个位置复制到用户定义位置,所以我使用了打开文件对话框。但我希望当用户点击按钮时,他只能选择复制文件的位置。我不希望用户选择我想要复制的文件?可以在打开的文件对话框中吗?

i am working on winform c# and i am stuck in open file dialog as i am dealing with it first time i want two files to be copied from one location to the user define location so i used open file dialog. but i want that when user clicks on the button he only gets the option where to copy the file. i dont want user to select the file which i want to copy? is that possible in open file dialog?

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "MDF Files|*.mdf|LDF Files|*.ldf";
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        // what i have to write here to select a file before the open file dialog opens so that user only gets the option to copy the file
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }
}

推荐答案

使用 FolderBrowserDialog [ ^ ]。这样,用户可以在不选择特定文件的情况下选择路径。



此代码不是100%完整,但它应该让您知道您需要什么do。

Instead of using an OpenFileDialog, use the FolderBrowserDialog[^]. That way the user can select a path without selecting a specific file.

This code is not 100% complete, but it should give you an idea of what you need to do.
string sourcePath = @"c:\\SourceFolder\";
string fileName1 = @"YourFileName1.txt";
string fileName2 = @"YourFileName2.txt";
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 );
    }
}


这篇关于使用打开的文件对话框将两个文件从一个位置保存到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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