Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
ı have a listbox populate with files ı try copy in listbox specified directory for example desktop or documents as how ı can copy specified directory

PLEASE LOOK COPY AS PİCTURE

http://www.image-upload.net/di-QIBQ.jpg[^]
Posted
Updated 20-Apr-12 10:26am
v2
Comments
Nelek 20-Apr-12 16:26pm    
Edit: Made clickable link
[no name] 20-Apr-12 18:55pm    
Your question and comments do not make any sense. You have not shown how you are populating any listbox with "files" nor have you specified how you are getting any directory to copy files to. And your graphic clearly shows a menu not a listbox.

Hello cebe_noyan,

I did not get you question properly but here is my interpretation of your problem:

You want to copy files specified (source files)in a listbox to a specific directory(target location). I take that the path of the source files if given.

Here's the solution i propose:
C#
using System.IO;

string sourceDir = "C:\\Test";
string target = "D:\\Test\\";

string[] sDirFiles = Directory.GetFiles(sourceDir);
if (sDirFiles.Length > 0)
{
     foreach (string file in sDirFiles)
     {
          string[] splitFile = file.Split('\\');
          string copyFile = Path.GetFileName(file);
          string source = sourceDir + "\\" + copyFile;
          Copy(source,target);
     }
}

public void Copy(string source, string target)
{
     try
     {
          //If file exists at the target location, then delete it
          if (File.Exists(target))
          {
               File.Delete(target);
          }
          //Otherwise copy it from source to target
          File.Copy(source, target);
     }
     catch (Exception ee)
     {
                
     }
}


I sincerely hope it helps. If it isn;t what you are looking for, would you explain your question a bit more?

Thanx,
Umer
 
Share this answer
 
v4
Comments
cebe_noyan 20-Apr-12 13:39pm    
how ı can get listbox items so files ı make string sourceDir = listbox1.text; with can ı get items...
Umer Aziz Malik 20-Apr-12 17:57pm    
What exactly r u storing in the ListBox? Are those the just the filenames?
Umer Aziz Malik 20-Apr-12 18:00pm    
It would be helpful if you could share how you are populating the ListBox
cebe_noyan 21-Apr-12 10:27am    
ı make so string a=listbox.text throw error the file name path null such as ı can t get to path throw error so from listbox all get paths and copy specified path..
Umer Aziz Malik 21-Apr-12 13:57pm    
can u please post the code here?
Note: another way you can move a file is get the FileInfo:

FileInfo info = new FileInfo(SourceFileName);
info.CopyTo(destFileName);


This may be a little more effecient since only have to get the inforamtion once. Still have to worry about destination already having a file with this name.
 
Share this answer
 
Comments
cebe_noyan 21-Apr-12 10:29am    
yes ı try but ı get all files to paths from listbox and specified path copy such as up the picture how ı can get all file paths..
Member 10364700 31-Oct-13 3:40am    
But I want to convert multiple image files to pdfs before moving it to the destination folder kindly help!!. I have used pdfsharp, but I can only convert it file by file specifying the souce and the destination paths. my code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;
namespace ConsoleApplication2
{
class Program
{

static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);

XImage img = XImage.FromFile(source path....);


xgr.DrawImage(img,0,0);
doc.Save(destinationpath....);
doc.Close();
}
}
}
Member 4159567 28-Sep-12 3:00am    
hi,

i want to copy files from source to destination, i have a issue that is if the destination folder already have a file then it throws a error.

So what i think is need to get the files from the destination folder and then delete them, then again have to create the files into that folder. but here i m really don't know how to handle this situation. can you suggest me if you have any idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900