Solved

C# copy files from one folder to another

Posted on 2009-05-11
7
4,066 Views
Last Modified: 2012-06-27
I have a gridview that lists all of the files in a folder. The gridview also shows a checkbox next to each file. What I need to accomplish is giving the use the ability to check any nunber of files listed, then when they click on the Copy button, the checked files will be copied to another folder. I've had it working in that every file is copied, but I need it to only be the selected files. The other feature needed is once the Copy button is clicked, the uses is asked where to copy the files to before they are copied. Is there any way to do all of this? I've been utilizing this site quite a bit lately. Thanks for all of the help. I provided code for the copy button below, but it's not complete yet, obviously.
public void copyButton(object sender, EventArgs e)
{
for (int i=0; i < this.GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked)
{
DataKey keyForThisRow = GridView1.DataKeys[i];
string filename = (string)keyForThisRow[0];
}
}
}

Open in new window

0
Comment
Question by:dodgerfan
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 24357460
Something like this.. you will need to make sure that the dialog comes back with a valid result.. and maybe do some checking to see if you want to overwrite.. etc.

You may need the namespace System.IO if you don't already have it.
public void copyButton(object sender, EventArgs e)
{
for (int i=0; i < this.GridView1.Rows.Count; i++)
{
   CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
   if (cb.Checked)
   {
      DataKey keyForThisRow = GridView1.DataKeys[i];
      string filename = (string)keyForThisRow[0];
     SaveFileDialog fileDialog = new SaveFileDialog();
     DialogResult result = fileDialog.ShowDialog();
     File.Copy(filename,fileDialog.FileName); //where filename and destFileName are full paths "C:\folder\filename.txt
   }
}
}

Open in new window

0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 250 total points
ID: 24357564
Copying only Selected Files
The code snippet provided may work correctly if you place the file copying code just after obtaining the file name. Make sure that the variable "filename" should be fully-qualified local or network path (UNC-based) of the source file rather than only the name. For copying a single file use the provided in code snippet.

Confirming the Target Path
For this purpose, you may have to utilize another page or same page or some modular pop up to host list/browse/selection of target paths available within the server machine where the files could be copied.

File.Copy("SourceFilePathAndNameWithExtension", "TargetFilePathAndNameWithExtension")
'With forced overwrite
File.Copy("SourceFilePathAndNameWithExtension", "TargetFilePathAndNameWithExtension", True)

Open in new window

0
 

Author Comment

by:dodgerfan
ID: 24357794
RPCIT,
I am using System.IO,  but it still does not recognize it. The error I get asks if I'm missing a reference. I am doing all of this wilt ASP.Net and C#.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:dodgerfan
ID: 24358341
I have the code working now in that only the files that are checked get copied. The part I am missing is having the user select where to put the files they want copied.
0
 
LVL 6

Assisted Solution

by:RPCIT
RPCIT earned 250 total points
ID: 24358394
oh .. asp.net..  I was thinking winforms.

In order you get the path.. you will have to create a control, or textbox, or something to ask for that.   Maybe show a control after the save button, asking for the path.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24359373
As I already mentioned in my post above that you may have to:

Create a section of the same page that may appear below the grid or in a modular popup such as ThickBox (http://jquery.com/demo/thickbox/) or ModalPopup (http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ModalPopup/ModalPopup.aspx) where the paths available for copying the files be displayed in a listbox or a custom control which may also allow browsing of various paths on the server (see the attached screen shot DotNetPanel popup path browser). Make use of appropriate validators to force user to select a target path before clicking Copy Files button.

OR

Create another page or make a hidden area of the same page visible on the press of Copy Files on postback where you'll receive the list of files (maybe a comma separated string) to be copied (this means you would not run File.Copy code in the loop). The list can be saved in a Session variable or some hidden field. Present the user with a list/control as mentioned in the above option 1 along with a button to Complete Copy or whatever caption you like and run the actual File.Copy code on the click event of this button.

For viewing the DotNetPanel in action, visit this demo page: http://www.dotnetpanel.com/dotnetpanel/demo/. Go to File Manager after choosing a User Space on logging into the panel.
DNP-File-Manager-Copy-Dialog-1.jpg
DNP-File-Manager-Copy-Dialog-2.jpg
0
 

Author Closing Comment

by:dodgerfan
ID: 31580253
Thanks for the help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question