how to delete file from directory in c#

please help me.

Last updated:7/7/2018 3:38:45 AM

2 Answers

Aditya Patel
Aditya Patel

use following code for deleting file and directory both.

        try
            {
                var FilePath=@"C:\\Temp\\ABC.TXT";
                if (System.IO.File.Exists(FilePath))
                {
                    System.IO.File.Delete(FilePath);
                    Directory.Delete(FilePath.Substring(0, FilePath.LastIndexOf('\\')));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            } 

Prakash nidhi Verma
Prakash nidhi Verma

Use this below code :
public class SimpleFileMove

{
    static void Main()
    {
        string sourceFile = @"C:\Users\Public\public\mindstick.txt";
        string destinationFile = @"C:\Users\Public\private\mindstick.txt";
        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);
        // To move an entire directory. To programmatically modify or combine
        // path strings, use the System.IO.Path class.
        System.IO.Directory.Move(@"C:\Users\Public\public\mindstick\", @"C:\Users\Public\private");
    }

Answer