Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# copy files from one folder to another

Posted on 2009-05-11
7
4,079 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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