Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2007, 05:40 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Adding ToString() to avoid a compiler error unless you understand what the compiler error actually means can be a bad thing.

I think you need to break down your program into steps, and add comments so you understand what each step is doing.

Code:
// Get the source directory from the textbox
string sourceDirectory = txtsrce.Text;

// Check to see if the directory actually exists
bool doesSourceExist = Directory.Exists(sourceDirectory);
if( !doesSourceExist ) 
{
  // Display a message and exist if the source directory doesn't exist
  MessageBox.Show("Source directory doesn't exists");
  return;
}

// Check the destination directory
string destDirectory = txtdest.Text;
bool doesDestExist = Directory.Exists(destDirectory);
if( !doesDestExist ) 
{
  // Create the destination directory if it doesn't already exist
  Directory.CreateDirectory(destDirectory);
}

// Loop through each file
foreach(string sourceFile in Directory.GetFiles(sourceDirectory) )
{
   // Calculate the full paths for the source and destination files
   string fullSourcePath = Path.Combine(sourceDirectory, sourceFile);
   string fullDestPath = Path.Combine(destDirectory, sourceFile);
   // copy the file
   File.Copy(fullSourcePath, fullDestPath);
}

/- Sam Judson : Wrox Technical Editor -/
 
Old November 6th, 2007, 01:54 AM
Authorized User
 
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to saidan Send a message via Yahoo to saidan
Default

Thank you very much;

i'm getting confusing in one thing;

when the folder is to be copied, the new folder in destination has to have same attributes of the original folder, that is same name and so on;

in the program i'll have to get the parent directory of the original folder and then start with the name of the folder and create the folder with the same name in the destination , then enter inside the folder and copy the entire files....



================================================
toward a success civelization;
 
Old November 6th, 2007, 04:54 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Can you illustrate what you are trying to do using an example.

e.g.

Directory C:\Temp\MyFolder contains two files, one.txt and two.pdf.

What will txtdest.Text contain, and what will the final directory structure look like?

/- Sam Judson : Wrox Technical Editor -/
 
Old November 8th, 2007, 12:49 AM
Authorized User
 
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to saidan Send a message via Yahoo to saidan
Default

>>

txtdist.text will contains the destenation directory, for example

c:\\Ducuments and settings\saidan\desktop

then, when you click on the copy button, you can see on Desktop the folder MyFolder contains two files, one.txt and two.pdf.

that's it.

:)

================================================
toward a success civelization;
 
Old November 8th, 2007, 07:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Simply append the source directory name onto the destination path:

string destDirectory = Path.Combine(txtdest.Text, Path.GetFileName(sourceDirectory));



/- Sam Judson : Wrox Technical Editor -/
 
Old November 9th, 2007, 07:19 AM
Authorized User
 
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to saidan Send a message via Yahoo to saidan
Default

ok;

after i removed .ToString() so many errors appeared, as you said the datatype which i'm passing are already of string type..

there maybe some one more problem!

another thing: the statement which you provided gives all files inside the directory, as you see i used Drectory.GetParent(str_source);

and when i use the statement which you've written, i'll get a group of files and folders inside the parent directory of the folder wich i choose, but i need only that folder to be copied and its intire files and folders as well!

>>:)

================================================
toward a success civelization;
 
Old November 9th, 2007, 07:51 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I've no idea why you would need to get the parent of the source directory.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 10th, 2007, 01:28 AM
Authorized User
 
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to saidan Send a message via Yahoo to saidan
Default

>>>


i have two options;

First: getting the parent, fron that get the folder which i need And aaply the operation on it..

Secound: getting the name and attributes of the folder which i choose, creating new folder with same name and attributes and finally do the operation of copying intier files..// but because i've no idia about the secound, i'm frying to follow the first...


>>>:)

================================================
toward a success civelization;
 
Old November 10th, 2007, 06:08 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

My example code above does the second of these for you.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
local folder location euschenk Classic ASP Basics 3 March 5th, 2008 04:46 PM
Project Folder Location ag19702004 Visual Studio 2005 0 March 14th, 2007 12:25 PM
opening a folder location JonniP Access 6 January 5th, 2007 09:01 AM
How to copy data at a location pointed by IntPtr srinvas40 C# 0 August 13th, 2006 05:03 AM
Copy data from a databse field to a folder Sheikha Oracle 1 December 15th, 2005 01:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.