-1

I am creating an upload page for my sample program in ASP.NET. Now I am uploading student documents certificates,marksheets in my local storage and I am saving that file in specific directory.D:\Vinayak\Student\Profiledocs\610\myfile.txt Upload was worked successfully.

Now I am delete a specific student profile documents in my local storage using my ASP.NET page.

D:\Vinayak\Student\Profiledocs\610\myfile.txt is my file storage directory.

Now how I am to delete this sub folder and files \610\myfile.txt.

610 is a column value named by studentid in my student details table.

If I am select any student id like 610 or 12 whatever it is.

The student files are saved in my parent folder Profiledocs. like this

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

Now what I am make to delete the sub folders and files in my parent folder Profiledocs

Dale K
  • 25,246
  • 15
  • 42
  • 71
vinayak vk
  • 115
  • 1
  • 15
  • 2
    `Directory.Delete(@"D:\Vinayak\Student\Profiledocs\610", true);`? - remove `\610` folder with all its content? – Dmitry Bychenko Nov 14 '18 at 06:52
  • Possible duplicate https://stackoverflow.com/questions/3342042/how-to-delete-subdirectories and https://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory – Hamza Haider Nov 14 '18 at 06:52
  • how your student id created? – er-sho Nov 14 '18 at 06:54
  • so you want to delete the entire 610 folder basically, not just that file? – Kush Nov 14 '18 at 06:56
  • Possible duplicate of [How to delete all files and folders in a directory?](https://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory) – string.Empty Nov 14 '18 at 07:08
  • @NicolasTyler i already seen that [How to delete all files and folders in a directory?] but it wont use for me. – vinayak vk Nov 14 '18 at 07:17
  • @vinayakvk Yet your selected answer is a slight variant of that answer. You can't expect the StackOverflow community to do your work for you. Please read through this before asking another question: https://stackoverflow.com/tour – string.Empty Nov 15 '18 at 07:17

2 Answers2

1

You can use below method to delete sub folder and its content by passing studentId as parameter.

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

You can use above function like

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

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

Note: Make sure that your upload folder have full access control to read and write.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
er-sho
  • 9,581
  • 2
  • 13
  • 26
0

So, you have the full file path, and you wish to delete its current directory. First you need to get the directory path.

fileDirectory = System.IO.Path.GetDirectoryName(filename)

This piece of code below will delete all folders and files in your current directory.

System.IO.DirectoryInfo di = new DirectoryInfo(fileDirectory);

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
    dir.Delete(true); 
}
Kush
  • 438
  • 1
  • 7
  • 20
  • If "you wish to delete its current directory", why the `foreach` loops and not just `new DirectoryInfo(fileDirectory).Delete(true);`? – Rufus L Nov 14 '18 at 07:05