Delete All Files in C#


This examples shows how to delete all files (*.*) from a folder in C#.
First, you need to get the list of file names from the specified directory (using static method Directory.Get­Files. Then delete all files from the list.
Delete all files in C#

[C#]
using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
foreach (string filePath in filePaths)
  File.Delete(filePath);

Delete all files (one-row example)
To delete all files using one code line, you can use Array.ForEach with combination of anonymous method.
[C#]
Array.ForEach(Directory.GetFiles(@"c:\MyDir\"),
              delegate(string path) { File.Delete(path); });

Get Application Directory [C#]
Following examples show how to get application or assembly folder.
Directory of windows forms application (.exe) in C#

Class Application in System.Window­s.Forms namespace has static property ExecutablePath. It contains path of the .exe file (that started the application) including the executable file name. To get only the folder part of the path, use static method GetDirectoryName of Path class.
[C#]
using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
 
Directory of any loaded assembly (.exe or .dll) in C#

First get reference to the assembly. You can use static methods of Assembly class. To get assembly of currently executing code use method Assembly.GetE­xecutingAssem­bly. To get assembly in which the specified class is defined use method Assembly.GetAs­sembly (with the specified class type as a paramater). The assembly must be loaded. Next get assembly file path using Assembly.CodeBase property.
[C#]
using System.IO;
using System.Reflection;

string path = Path.GetDirectoryName(
                     Assembly.GetAssembly(typeof(MyClass)).CodeBase);

File Attributes [C#]

This example shows how to get or set file attributes, how to add attributes and how to remove attributes from current ones.
Get file attributes in C#

To get file attributes use static method File.GetAttri­butes. The method returns FileAttributes which is a bitwise combination of file attribute flags.
[C#]
string filePath = @"c:\TEXT.txt";
 
// get file attributes
FileAttributes fileAttributes = File.GetAttributes(filePath);
 
Set file attributes in C#

To set file attributes use static method File.SetAttri­butes. Parameter of the method is a bitwise combination of FileAttributes enumeration.
[C#]
// clear all file attributes
File.SetAttributes(filePath, FileAttributes.Normal);

// set just only archive and read only attributes (no other attribute will set)
File.SetAttributes(filePath, FileAttributes.Archive |
                             FileAttributes.ReadOnly);

Check whether a file has any attribute in C#

To check whether a file has any attribute (readonly, hidden) get current file attributes first and use bitwise AND (&) operator with a mask of specific attributes.
[C#]
// check whether a file is read only
bool isReadOnly = ((File.GetAttributes(filePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
 
// check whether a file is hidden
bool isHidden = ((File.GetAttributes(filePath) & FileAttributes.Hidden) == FileAttributes.Hidden);

// check whether a file has archive attribute
bool isArchive = ((File.GetAttributes(filePath) & FileAttributes.Archive) == FileAttributes.Archive);

// check whether a file is system file
bool isSystem = ((File.GetAttributes(filePath) & FileAttributes.System) == FileAttributes.System);

Delete/clear file attributes from current ones in C#

To delete file attributes from the existing ones get the current file attributes first and use AND (&) operator with a mask (bitwise complement of desired attributes combination).
[C#]

// delete/clear hidden attribute
File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.Hidden);

// delete/clear archive and read only attributes
File.SetAttributes(filePath, File.GetAttributes(filePath) & ~(FileAttributes.Archive |
                                                              FileAttributes.ReadOnly));

Get File Time [C#]

This example shows how to get file time informations, when any file was created, last modified or accessed. To get file datetime info you can use either static methods of File class or instance methods of FileInfo class.
Get file times using File class in C#
Use File class when you want to get just one specific time, for example if you are only interested in a file last modification time. To do this use static method File.GetLastWri­teTime with file path as a parameter. File class also provides static methods to get file creation time or file last access time. You can also get this times in UTC, e.g. to get file last write time in UTC use File.GetLastWri­teTimeUtc.
[C#]
// local times
DateTime creationTime = File.GetCreationTime(@"c:\TEXT.txt");
DateTime lastWriteTime = File.GetLastWriteTime(@"c:\ TEXT.txt");
DateTime lastAccessTime = File.GetLastAccessTime(@"c:\ TEXT.txt");

// UTC times
DateTime creationTimeUtc = File.GetCreationTimeUtc(@"c:\ TEXT.txt");
DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(@"c:\ TEXT.txt");
DateTime lastAccessTimeUtc = File.GetLastAccessTimeUtc(@"c:\ TEXT.txt");

// write file last modification time (local / UTC)
Console.WriteLine(lastWriteTime);     // 9/30/2011 2:16:04 PM
Console.WriteLine(lastWriteTimeUtc);  // 9/30/2011 6:16:04 PM

Get file times using FileInfo class in C#

Use instance of FileInfo class when you want to get more than one file time or any other informations about the file (like file attributes). Advantage is that you will get all needed informations just in one disk access. See following example.
[C#]
FileInfo fileInfo = new FileInfo(@"c:\file.txt");

// local times
DateTime creationTime = fileInfo.CreationTime;
DateTime lastWriteTime = fileInfo.LastWriteTime;
DateTime lastAccessTime = fileInfo.LastAccessTime;

// UTC times
DateTime creationTimeUtc = fileInfo.CreationTimeUtc;
DateTime lastWriteTimeUtc = fileInfo.LastWriteTimeUtc;
DateTime lastAccessTimeUtc = fileInfo.LastAccessTimeUtc;
 
// write file last modification time (local / UTC)
Console.WriteLine(lastWriteTime);     // 9/30/2011 2:16:04 PM
Console.WriteLine(lastWriteTimeUtc);  // 9/30/2011 6:16:04 PM

Open File With Associated Application [C#]

This example demonstrates how to open file with an associated program. It shows, how to open text document in notepad, how to open image in a default viewer or how to open url address in a default web browser.
Applications are launched using Process.Start method. The file path or url is passed as a parameter.
[C#]
// open text file in notepad (or another default text editor)
System.Diagnostics.Process.Start(@"c:\textfile.txt");
[C#]
// open image in default viewer
System.Diagnostics.Process.Start(@"c:\image.jpg");
[C#]
// open url in default web browser
System.Diagnostics.Process.Start("http://www.csharp-examples.net");
[C#]
// open PDF file
System.Diagnostics.Process.Start(@"c:\document.pdf");

Similarly you can open Word document or any other file from your .NET application.



0 comments: