问个C#文件夹处理问题,希望高手回答下,感激不尽
一个文件夹里面有一个子文件夹和一个C#文件,子文件夹里面有一些文件,各种类型都有,我想实现点击C#文件的按钮,将子文件里面的内容复制到和C#文件同一目录!其详细代码,谢谢...
一个文件夹里面有一个子文件夹和一个C#文件,子文件夹里面有一些文件,各种类型都有,我想实现点击C#文件的按钮,将子文件里面的内容复制到和C#文件同一目录!
其详细代码,谢谢了 展开
其详细代码,谢谢了 展开
3个回答
展开全部
使用
System.IO.Directory
System.IO.DirectoryInfo
System.IO.File
System.IO.FileInfo
的相关方法来完成!
简例:
string[] fileList =System.IO.Directory.GetFiles(DirPath);
//DirPath 子文件夹路径
foreach (string file in fileList)
{
System.IO.File.Copy(file, Path + System.IO.Path.GetFileName(file),true);
//Path 目标文件夹路径 true 覆盖同名文件
}
System.IO.Directory
System.IO.DirectoryInfo
System.IO.File
System.IO.FileInfo
的相关方法来完成!
简例:
string[] fileList =System.IO.Directory.GetFiles(DirPath);
//DirPath 子文件夹路径
foreach (string file in fileList)
{
System.IO.File.Copy(file, Path + System.IO.Path.GetFileName(file),true);
//Path 目标文件夹路径 true 覆盖同名文件
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileCopy
{
class Program
{
static void Main(string[] args)
{
try
{
StartCopy();
}
catch (Exception ex)
{
ShowMessage("复制时发生错误:");
ShowMessage(ex.Message, true);
}
}
static void StartCopy()
{
//取得程序所在目录。
String appDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//取得当前目录里面的子文件夹。
String[] childDir = Directory.GetDirectories(appDir);
if (childDir.Length == 0)
{
ShowMessage("当前目录没有子文件夹。", true);
return;
}
else if (childDir.Length > 0)
{
ShowMessage("当前目录中的子文件夹超过一个。", true);
return;
}
String[] files = Directory.GetFiles(childDir[0], "*", SearchOption.AllDirectories);
ShowMessage(String.Format("共找到{0}个文件。", files.Length));
Int32 fail = 0;
Int32 success = 0;
foreach (String file in files)
{
String fileName = Path.GetFileName(file);
String destPath = Path.Combine(appDir, fileName);
if (File.Exists(destPath))
{
ShowMessage(String.Format("目标目录已经存在同名文件“{0}”,跳过。", fileName));
fail++;
}
else
{
ShowMessage(String.Format("正在复制文件“{0}”……", file));
File.Copy(file, destPath);
success++;
}
}
ShowMessage(String.Format("复制完成,成功{0}个,失败{1}个。", success, fail), true);
}
static void ShowMessage(String message)
{
ShowMessage(message, false);
}
static void ShowMessage(String message, Boolean pause)
{
Console.WriteLine(message);
if (pause)
{
Console.WriteLine("按任意键继续……");
Console.ReadKey();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询