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 October 31st, 2007, 03:23 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 Copy A folder to another location

>>

i tried to copy a folder from one location entered by the user tp another one which is taken from the user also;

i tried by the following code but exception appeared:

string str_src = "@" + (txtsrce.Text);
                DirectoryInfo d_nfo=new DirectoryInfo( str_src);
                bool src = d_nfo.Exists;
                if(src)
                {
                    MessageBox.Show("Directory Exist");
                    FileInfo[] files = d_nfo.GetFiles();
                    string str_dist = "@"+(txtdest.Text);
                    DirectoryInfo d_n_dist=new DirectoryInfo(str_dist);
                    FileInfo[] file_d= d_n_dist.GetFiles();
                    bool dist = d_n_dist.Exists;
                    if(dist)
                    {
                       foreach(FileInfo file in files)
                       {
                            File.Copy(file.ToString(),file_d.ToString());
                       }
                    }
                    else
                        MessageBox.Show("The distination is not exist");
                }
                MessageBox.Show("The source Dir is incorrect");

The exception is:The given Path Format is not Supported

may i know the wrong statement in my code?!



================================================
toward a success civelization;
__________________
================================================
toward a success civelization;
 
Old October 31st, 2007, 04:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

string str_src = "@" + (txtsrce.Text);

Whats this meant to be? Directory names do not start with a @ symbol.

Are you getting it confused with this way of writing string literals:

string str_src = @"C:\Temp";

Note, the '@' is outside the string, it is not part of the string. It is simply a way of marking the string as an unescaped string literal.

/- Sam Judson : Wrox Technical Editor -/
 
Old October 31st, 2007, 10:40 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;
 now the statement bool src = d_nfo.Exists; returns false value
and the MessageBox.Show("The source Dir is incorrect");
is appear...

there might be a problem in the statement itself>>

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

Looking at your code the statement will always run:

if(src) {
  // only run if src = true
}
// always run
MessageBox.Show("The source Dir is incorrect");

It looks like you have got your 'else' in the wrong place.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 2nd, 2007, 01:53 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'm choosing the directory using OpenFileDialog, that means the directory shuold be correct ant the condition returns true value,

and then at least the MessageBox.Show("Directory Exist"); will appear...

but always i'm getting the MessageBox.Show("The source Dir is incorrect"); statement appears!



================================================
toward a success civelization;
 
Old November 2nd, 2007, 02:41 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, the problem of str is solved;

but the new question is how can i get the list of the files in the directory in order to copy them using file.copy() method?

the way which i have is FolderNmar.GetFiles(), but this takes all files..

2- are there any method to make an id for each file inside the folder?

:)

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

First, a small gripe of mine: your variable names are a little cryptic. There is no need to call things "d_n_fls" when "destinationFiles" would do much better. It makes it easier to read, for you and for us.

I'm not quite sure what your problem is. You say you want to list the files in a directory, and then that GetFiles() returns all the files. What appears to be the problem?

/- Sam Judson : Wrox Technical Editor -/
 
Old November 2nd, 2007, 07:17 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

this is the latest program:

string str_source = (txtsrce.Text);
                DirectoryInfo source_info=new DirectoryInfo(Directory.GetParent( @str_source).ToString());
                bool source = Directory.Exists(source_info.ToString());
                if(source)
                {
                    MessageBox.Show("Directory Exist");
                    string str_distination = (txtdest.Text);
                    DirectoryInfo DirectoryInfoDestenation=new DirectoryInfo(@str_distination);
                    //FileInfo[] file_d= d_n_dist.GetFiles();
                    bool dist = DirectoryInfoDestenation.Exists;
                    if(dist==true)
                    {
                        MessageBox.Show("The Folder Is Already There, wold you like To Replace it", MessageBoxButtons.OKCancel.ToString());
                    }else{
                        Directory.CreateDirectory(DirectoryInfoDestenation .ToString());
                        FileInfo []files = source_info.GetFiles();
                        //FileInfo files = d_nfo.GetFiles();
                        foreach(FileInfo file in files)
                        {
                            string F_Out = DirectoryInfoDestenation + ("'\'") + file.Name;
                            MessageBox.Show(F_Out.ToString());
                            File.Copy(file.Name, F_Out.ToString());
                            //file.CopyTo(d_n_dist.ToString());

                        }
                    }

                }
                else //if (!src)
                {
                    MessageBox.Show("The source Dir is incorrect");
                }

The problem is: sometimes it lists the files of the parent directory of the folder which i have shosen, the thing which makes exception;

i suppose to get the name of eash file to pass it to File.Copy(), in order to copy it to the destination folder..

..:)

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

OK, firstly, you've still got a load '@' in front of variables. You don't need them, so get rid of them (it makes you've code cleaner).

Likewise, you're adding .ToString() onto the end of a load of things that are already strings. e.g. Directory.GetParent() returns a string - there is no need to add ToString() on the end. F_Out is already a string, don't add F_Out.ToString().

You are doing this also with the MessageBoxOptions.OkCancel (no ToString!) - this means your code is dong something you don't want it to, i.e. it is setting the Caption to your message box to "OkCancel", rather than showing an OK and a Cancel box.

Thirdly, why not just search google for "C# copy directory" and copy and paste the code here: http://www.codeproject.com/cs/files/...srecursive.asp

/- Sam Judson : Wrox Technical Editor -/
 
Old November 5th, 2007, 01:47 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 for your comments;

"@" and the things which i tried to convert to string are because of come errors which was appearing and not after adding this Tostring();

secound thing thats fourth in your reply, i have my own misunderstanding, i need to clear my them..

NO benefit from Copy Past!

>>:)

================================================
toward a success civelization;





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.