问题描述
ı有一个用文件填充的列表框ı尝试在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[^]
推荐答案
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 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#将多个文件从列表框复制到指定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!