Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# copy files from one folder to another

Posted on 2009-05-11
7
Medium Priority
?
4,161 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
[X]
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
  • 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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