Solved

C# copy files from one folder to another

Posted on 2009-05-11
7
4,045 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:dodgerfan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

12 Experts available now in Live!

Get 1:1 Help Now