Click here to Skip to main content
11,505,158 members (68,727 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 31-Jan-13 3:59am
sariqkhan1.4K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 );
    }
}
  Permalink  
v2
Comments
sariqkhan at 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 at 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?
Marcus Kramer at 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 at 31-Jan-13 10:51am
   
My 5. Oh, takes patience...
—SA
Marcus Kramer at 31-Jan-13 11:02am
   
Thanks, Sergey. I must have gotten up on the right side of the bed thing morning. Cheers.
sariqkhan at 31-Jan-13 11:40am
   
sir can you provide me the simple code. That link contains extra code which i didint get. Please if you can
sariqkhan at 31-Jan-13 13:06pm
   
thank you sir.
and if i want to reverse the procedure for example i have two file somewhere in the computer i want user to select those file persnally and user should select two files and if clicked okay then that two files will be placed in the specified location which means c\programfiles\mylocation. this can be done by folderbrowse dialog?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 403
1 F-ES Sitecore 355
2 Richard Deeming 270
3 CHill60 195
4 Richard MacCutchan 190
0 Sergey Alexandrovich Kryukov 723
1 Peter Leow 490
2 Maciej Los 475
3 DamithSL 470
4 OriginalGriff 437


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100