Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ben25 reputationPosted Sep 8, 2009, 7:18 AM EDT
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.
String filePath = ""; OpenFileDialog getFile = new OpenFileDialog(); SaveFileDialog saveFile = new SaveFileDialog(); DialogResult dg = getFile.ShowDialog(); if (dg == DialogResult.OK) { filePath = getFile.FileName; saveFile.FileName = filePath; } if (filePath != "") { dg = saveFile.ShowDialog(); if (dg == DialogResult.OK) { if (System.IO.File.Exists(filePath)) { System.IO.File.Copy(filePath, saveFile.FileName); } } }Roei BarRank #1,271 · 1.4K reputationPosted Sep 8, 2009, 12:07 PM EDT
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 );
}
}
}
}