Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
    }
}
Posted

1 solution

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.
C#
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 );
    }
}
 
Share this answer
 
v2
Comments
sariqkhan 31-Jan-13 10:06am    
thank you sir but in that folderbrowsedialog. can i get option to copy files? and i dont want to move folder as folder contains many things i only want two files to be copied in another location. can you please provide some help
sariqkhan 31-Jan-13 10:09am    
sir i think you misunderstood my question, i have two file in a folder c\programfiles\myfolder i want that when the button is clicked then the two files is selected automatically and the user only gets the option to where to copy this two files. like that way i want to work. is that possible what i am thinking?
fjdiewornncalwe 31-Jan-13 10:49am    
That is exactly what the FolderBrowserDialog is for. This dialog will allow the user to select a folder and then when they press ok, you handle that in your code and write your file copy code. The Dialog itself does nothing more than what its name implies, it allows the user to select a folder.
Sergey Alexandrovich Kryukov 31-Jan-13 10:51am    
My 5. Oh, takes patience...
—SA
fjdiewornncalwe 31-Jan-13 11:02am    
Thanks, Sergey. I must have gotten up on the right side of the bed thing morning. Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900