c#将多个文件从列表框复制到指定的文件夹 [英] c# copy multiple files from listbox to specified folder
问题描述
ı有一个用文件填充的列表框ı尝试在listbox指定的目录(例如桌面或文档)中复制,如ı如何复制指定的目录
请查看作为复制品的副本
http://www.image-upload.net/di-QIBQ.jpg [ ^ ]
ı 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[^]
推荐答案
你好cebe_noyan,
我没有正确回答您的问题,但这是我对您的问题的解释:
您要将列表框中指定的文件(源文件)复制到特定目录(目标位置).如果给出的话,我会采用源文件的路径.
这是我建议的解决方案:
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:
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)
{
}
}
我衷心希望它能对您有所帮助.如果不是您要找的东西,您能再解释一下您的问题吗?
谢谢,
Umer
I sincerely hope it helps. If it isn;t what you are looking for, would you explain your question a bit more?
Thanx,
Umer
注意:另一种移动文件的方法是获取FileInfo:
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.
这篇关于c#将多个文件从列表框复制到指定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!