Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

How to delete folder with files on c#

Directory.Delete(@"folderPath", true);
Comment

C# delete folder with all contents

System.IO.Directory.Delete("C:TempTest", True)
Comment

c# delete files in directory and subdirectories

 public static void RecursiveDelete(DirectoryInfo parentDir)
        {
            if (parentDir.Exists)
            {
                foreach (var dir in parentDir.EnumerateDirectories())
                {
                    RecursiveDelete(dir);
                }

                var files = parentDir.GetFiles();
                foreach  (var item in files)
                {
                    item.IsReadOnly = false;
                    item.Delete();
                }

                parentDir.Delete(true);
            }
        }
Comment

c# delete files

// Simple synchronous file deletion operations with no user interface.
// To run this sample, create the following files on your drive:
// C:UsersPublicDeleteTest	est1.txt
// C:UsersPublicDeleteTest	est2.txt
// C:UsersPublicDeleteTestSubDir	est2.txt

public class SimpleFileDelete
{
    static void Main()
    {
        // Delete a file by using File class static method...
        if(System.IO.File.Exists(@"C:UsersPublicDeleteTest	est.txt"))
        {
            // Use a try block to catch IOExceptions, to
            // handle the case of the file already being
            // opened by another process.
            try
            {
                System.IO.File.Delete(@"C:UsersPublicDeleteTest	est.txt");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }

        // ...or by using FileInfo instance method.
        System.IO.FileInfo fi = new System.IO.FileInfo(@"C:UsersPublicDeleteTest	est2.txt");
        try
        {
            fi.Delete();
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }

        // Delete a directory. Must be writable or empty.
        try
        {
            System.IO.Directory.Delete(@"C:UsersPublicDeleteTest");
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }
        // Delete a directory and all subdirectories with Directory static method...
        if(System.IO.Directory.Exists(@"C:UsersPublicDeleteTest"))
        {
            try
            {
                System.IO.Directory.Delete(@"C:UsersPublicDeleteTest", true);
            }

            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        // ...or with DirectoryInfo instance method.
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:UsersPublicpublic");
        // Delete this dir and all subdirs.
        try
        {
            di.Delete(true);
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# get display resolution 
Csharp :: c# remove last value from list 
Csharp :: how to create directory folder in c# 
Csharp :: replace text c# file 
Csharp :: 2d unity point at 
Csharp :: unity scriptable object 
Csharp :: if animation ends 
Csharp :: move in the direction that player is facing unity 
Csharp :: c# reverse string 
Csharp :: c# main method 
Csharp :: c# post get request 
Csharp :: how to change the extension of a file C# 
Csharp :: headless chromedriver C# 
Csharp :: c# counting lines 
Csharp :: c# repeat string x times 
Csharp :: c# count specific element in list 
Csharp :: unity gameobject.findobjectswith tag set active 
Csharp :: ef database first generate models entity framework core 
Csharp :: read configuration workerservice 
Csharp :: addding two numebrs with c# 
Csharp :: c# conver date utc to cst 
Csharp :: snx turn off linux 
Csharp :: listview disable resize columns 
Csharp :: how to compare 2 date time in asp.net core 3.1 
Csharp :: blank background for button wpf 
Csharp :: struct constructor c# 
Csharp :: C# delete last enviroment new line 
Csharp :: c# initialize array 
Csharp :: blazor button onclick parameter 
Csharp :: c# stringbuilder to file 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =