1.###############################c#如何检测文件夹中有多少个文件夹(测试OK)
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:/Windows"); System.IO.DirectoryInfo[] dirs = dir.GetDirectories(); if (dirs.Length > 0) { //有子文件夹 } else { //没有子文件夹 }
2.###############################
//删除文件
for (int i = 0; i < Directory.GetFiles(directoryPath).ToList().Count; i++)
{
if (Directory.GetFiles(directoryPath)[i] == fileName)
{
File.Delete(fileName);
}
}
//删除文件夹
for (int i = 0; i < Directory.GetDirectories(directoryPath).ToList().Count; i++)
{
if (Directory.GetDirectories(directoryPath)[i] == fileName)
{
Directory.Delete(fileName, true);
}
}
3.###############################
if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
}
//Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//删除文件夹以及文件夹中的子目录,文件
//判断文件的存在
if (File.Exists(Server.MapPath("~/upimg/Data.html")))
{
Response.Write("Yes");
//存在文件
}
else
{
Response.Write("No");
//不存在文件
File.Create(MapPath("~/upimg/Data.html"));//创建该文件
}
string name = GetFiles.FileName;//获取已上传文件的名字
string size = GetFiles.PostedFile.ContentLength.ToString();//获取已上传文件的大小
4###################################################
C# 中判断文件及文件夹是否存在
2011-05-19 17:37:06| 分类: asp.net学习 | 标签:学习 |举报|字号 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;//引入所需要的类库
public partial class GetFileName : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PrintViewFilename();
CreateFile();
IfHaveFile()
}
/// <summary>
/// 输出一个文件下的所有目录
/// </summary>
private void PrintViewFilename()
{
DirectoryInfo path = new DirectoryInfo(@"F:\");//(@"F:\C#study\")改成(F:\\C#study\\)是一样的
FileInfo[] arr = path.GetFiles();
Literal1.Text = "";
for (int i = 0; i < arr.Length; i++)
{
Literal1.Text += "文件" + i.ToString() + arr[i].Name + "<br/>";
}
}
//判断一个文件夹是否存在,如果存在,就删除,如果不存就创建
private void CreateFile()
{
string pathstr = "F:\\wessss";
DirectoryInfo mypath = new DirectoryInfo(pathstr);
if (mypath.Exists)
{
mypath.Delete();
Response.Write("已经有一个名为wessss的文件<br/>");
}
else
{
mypath.Create();
Response.Write("已经创建了一个名为wessss的文件<br/>");
}
}
//此方法判断是否存在文件
private void IfHaveFile()
{
//
string MyfileNname = "F:\\wentextggg.txt";
if (MyfileNname.Length < 1)
return;
string ShortName = MyfileNname.Substring(MyfileNname.LastIndexOf("\\") + 1); //
//Substring(i)从第i个开始截取
//MyfileNname.LastIndexOf(i)最后一次出现i的位置
if (File.Exists(MyfileNname)) //调用File.Exists(string)方法判断是否存在
{
Response.Write("文件:" + ShortName + "已经存在!<br/><br/>");
}
else
{
Response.Write("文件:" + ShortName + "不存在!<br/><br/>");
}
}
}
5###############################################################
可以变通一下 先建立一个文件夹 然后将所有的文件拷贝过去
做了个例子
using System;
using System.IO;
internal static class Program
{
private static void Main()
{
// 文件夹路径(包括子文件夹)要以一个斜杠结尾,否则可能出问题
CopyFolder("D:\\doc\\", "E:\\doc\\");
}
private static void CopyFolder(string from, string to)
{
if (!Directory.Exists(to))
Directory.CreateDirectory(to);
// 子文件夹
foreach (string sub in Directory.GetDirectories(from))
CopyFolder(sub + "\\", to + Path.GetFileName(sub) + "\\");
// 文件
foreach (string file in Directory.GetFiles(from))
File.Copy(file, to + Path.GetFileName(file), true);
}
}
6#######################################################
拷贝文件夹的所有内容到另一个文件夹内:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
删除文件夹:
1 |
|
7##############################################
C#实现复制文件夹中文件到另一个文件夹的方法
private void CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加
if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
aimPath += System.IO.Path.DirectorySeparatorChar;
}
// 判断目标目录是否存在如果不存在则新建
if (!System.IO.Directory.Exists(aimPath))
{
System.IO.Directory.CreateDirectory(aimPath);
}
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
// string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (System.IO.Directory.Exists(file))
{
CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
}
// 否则直接Copy文件
else
{
System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
}
}
}
catch (Exception e)
{
throw;
}
}