please help me how to copying imagefile one folder to another folder with openfiledialog
please help me how to copying imagefile one folder to another folder with openfiledialog
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Roei BarPosted Sep 8, 2009, 4:07 PM
using System.IO;
namespace CSharp411
{
class Program
{
private static System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
static void Main( string[] args )
{
string _sourceFolder = string.Empty;
string _destinationFolder = string.Empty;
//Get Source Folder
folderBrowserDialog.RootFolder=System.Environment.SpecialFolder.MyComputer;
folderBrowserDialog.ShowNewFolderButton=false;
DialogResult result=this.folderBrowserDialog.ShowDialog();
if (result==DialogResult.OK)
{
// retrieve the name of the selected folder
_sourceFolder = folderBrowserDialog.SelectedPath;
}
//Get Destination Folder
folderBrowserDialog.RootFolder=System.Environment.SpecialFolder.MyComputer;
folderBrowserDialog.ShowNewFolderButton=false;
DialogResult result=this.folderBrowserDialog.ShowDialog();
if (result==DialogResult.OK)
{
// retrieve the name of the selected folder
_destinationFolder = folderBrowserDialog.SelectedPath;
}
//run a recursive copy of the source directory to the destination
//file and folders
CopyFolder( _sourceFolder, _destinationFolder );
Console.ReadLine();
}
static public void CopyFolder( string sourceFolder, string destFolder )
{
if (!Directory.Exists( destFolder ))
Directory.CreateDirectory( destFolder );
string[] files = Directory.GetFiles( sourceFolder );
foreach (string file in files)
{
string name = Path.GetFileName( file );
string dest = Path.Combine( destFolder, name );
File.Copy( file, dest );
}
string[] folders = Directory.GetDirectories( sourceFolder );
foreach (string folder in folders)
{
string name = Path.GetFileName( folder );
string dest = Path.Combine( destFolder, name );
CopyFolder( folder, dest );
}
}
}
}
BenPosted Sep 8, 2009, 11:18 AM
Then open a SaveFileDialog for the user to select the place and name where the file will be copied.
Hope it helps.
Let me know if you need explanations or modifications.