您的位置:首页 > 编程语言 > ASP

asp.net 文件夹和文件的创建、删除

2012-08-16 17:21 295 查看
View Code

/// <summary>
/// 用递归方法删除文件夹目录及文件
/// </summary>
/// <param name="dir">带文件夹名的路径</param>
public void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir, true); //删除已空文件夹
}
}

/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="Path"></param>
public void FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}


确保您具有足够的权限 对路径 的访问被拒绝

删除权限设置:
在web.config中的<system.web>下加入<identity impersonate="true"/>

删除文件夹下的文件

protected void ss232_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo path = new System.IO.DirectoryInfo("C:\\fingerPrint\\ss");
deletefile(path);
}

private void deletefile(System.IO.DirectoryInfo path)
{
foreach (System.IO.DirectoryInfo d in path.GetDirectories())
{
deletefile(d);
}
foreach (System.IO.FileInfo f in path.GetFiles())
{
f.Delete();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐