|
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
|
|
|
November 5th, 2007, 05:40 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
November 6th, 2007, 01:54 AM
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
|
November 6th, 2007, 04:54 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
November 8th, 2007, 12:49 AM
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
>>
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;
|
November 8th, 2007, 07:36 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Simply append the source directory name onto the destination path:
string destDirectory = Path.Combine(txtdest.Text, Path.GetFileName(sourceDirectory));
/- Sam Judson : Wrox Technical Editor -/
|
November 9th, 2007, 07:19 AM
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
|
November 9th, 2007, 07:51 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I've no idea why you would need to get the parent of the source directory.
/- Sam Judson : Wrox Technical Editor -/
|
November 10th, 2007, 01:28 AM
|
Authorized User
|
|
Join Date: Oct 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
>>>
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;
|
November 10th, 2007, 06:08 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
My example code above does the second of these for you.
/- Sam Judson : Wrox Technical Editor -/
|
|
|