Solved

C# copy files from one folder to another

Posted on 2009-05-11
7
4,052 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 48
Need to pass stored proc parameters in list 13 33
Stream.BeginRead and Stream.EndRead in .NET Core 5 38
Error in page 3 46
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now