How do I delete an existing file?

Category: System.IO, viewed: 326 time(s).
using System;
using System.IO;

namespace Kodecsharp.Example.System.IO {
    class FileDeleteDemo {
        public static void Main(string[] args) {
            string fileName = @"c:\users\me\demo.txt";

            //
            // To delete a file we can use the File.Delete() method and
            // pass the filename to be deleted as the parameter.
            //
            if (File.Exists(fileName)) {
                File.Delete(fileName);
            }

            //
            // We can also use the FileInfo class's Delete() method to
            // delete a file.
            //
            if (File.Exists(fileName)) {
                FileInfo fi = new FileInfo(fileName);
                fi.Delete();
            }
        }
    }
}
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Statistics

Locations of visitors to this page