Need to copy folder from one path to other
-
I have to find a folder from the path where my application exist e.g. c:/programfile/RTGSL/exe/.. then from exe folder i have to copy two folders(with all data) name applicationdata & client from this path to the current logged in user's personal folder path i.e. C:\Documents and Settings\ashishjaiswal\Application Data\clientdata\ if and only if no folder exist at this path.. Can any body help me with this code .. I am really new to programming
-
I have to find a folder from the path where my application exist e.g. c:/programfile/RTGSL/exe/.. then from exe folder i have to copy two folders(with all data) name applicationdata & client from this path to the current logged in user's personal folder path i.e. C:\Documents and Settings\ashishjaiswal\Application Data\clientdata\ if and only if no folder exist at this path.. Can any body help me with this code .. I am really new to programming
OK, this one is not as easy as you would like to think. As far as I know you need to do all the work here. Basically, you should first obtain you two folder paths. Application path can be obtained using...
string sourceDir = Application.StartupPath + "\\FolderToCopy";
Then you destination path should be available in the System.Environment.SpecialFolder enum, im not sure which one you will want to use thou so either test a few or google for answers...
string destDir = System.Environment.SpecialFolder.ApplicationData;//I guess this one is right
Now you need to create a recursive function that will copy a folder and all sub-folders and files. Keep in mind this is off top of my head so sorry if it does not work as it should...
void CopyFolder(string source, string destination)
{
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(source);//get info for the folder
System.IO.Directory.CreateDirectory(destination);//create the folderforeach(System.IO.FileInfo file in info.GetFiles())
{
file.CopyTo(destination + "\\" + file.Name);
}foreach(System.IO.DirectoryInfo dir in info.GetDirectories())
{
CopyFolder(dir.FullName, destination + "\\" + dir.Name);
}}
..sorry if it does'nt work properly, but im sure you can google for 'C# Copy Directory' and get the code you need
Life goes very fast. Tomorrow, today is already yesterday.
-
OK, this one is not as easy as you would like to think. As far as I know you need to do all the work here. Basically, you should first obtain you two folder paths. Application path can be obtained using...
string sourceDir = Application.StartupPath + "\\FolderToCopy";
Then you destination path should be available in the System.Environment.SpecialFolder enum, im not sure which one you will want to use thou so either test a few or google for answers...
string destDir = System.Environment.SpecialFolder.ApplicationData;//I guess this one is right
Now you need to create a recursive function that will copy a folder and all sub-folders and files. Keep in mind this is off top of my head so sorry if it does not work as it should...
void CopyFolder(string source, string destination)
{
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(source);//get info for the folder
System.IO.Directory.CreateDirectory(destination);//create the folderforeach(System.IO.FileInfo file in info.GetFiles())
{
file.CopyTo(destination + "\\" + file.Name);
}foreach(System.IO.DirectoryInfo dir in info.GetDirectories())
{
CopyFolder(dir.FullName, destination + "\\" + dir.Name);
}}
..sorry if it does'nt work properly, but im sure you can google for 'C# Copy Directory' and get the code you need
Life goes very fast. Tomorrow, today is already yesterday.
musefan wrote:
string destDir = System.Environment.SpecialFolder.ApplicationData;//I guess this one is right
Actually, it's
string destDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);