首页 > 解决方案 > c#中删除子文件夹

问题描述

我正在为 ASP.NET 中的示例程序创建一个上传页面。现在我将学生文档上传certificates,marksheets到本地存储中,并将该文件保存在特定目录中。D:\Vinayak\Student\Profiledocs\610\myfile.txt上传成功。

现在,我正在使用我的 ASP.NET 页面删除本地存储中的特定学生个人资料文档。

D:\Vinayak\Student\Profiledocs\610\myfile.txt是我的文件存储目录。

现在我如何删除这个子文件夹和文件\610\myfile.txt

610studentid是我的表中名为 by 的列值student details

如果我选择任何学生 ID,610 or 12无论它是什么。

学生文件保存在我的父文件夹Profiledocs中。像这样

\Profiledocs\610\myfile.txt
\Profiledocs\121\myfile1.txt
\Profiledocs\321\myfile2.txt

现在我要删除父文件夹中的子文件夹和文件Profiledocs

标签: c#

解决方案


studentId您可以使用以下方法通过作为参数传递来删除子文件夹及其内容。

private static void DeleteSubFolderAndContent(int studentId)
{
    string path = Path.Combine(@"D:\Vinayak\Student\Profiledocs", studentId.ToString());   
    Directory.Delete(path, true);
}

您可以使用上述功能

static void Main(string[] args)
{
    DeleteSubFolderAndContent(610); // <= "610" comes from database

    Console.WriteLine("Sub Folder and Its Content Deleted Successfully");
    Console.ReadLine();
}

注意:确保您的上传文件夹具有完全的读写权限控制。


推荐阅读