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.GetFiles. 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#
[C#]
using System.IO;
using System.Windows.Forms;
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Directory of any loaded assembly (.exe or .dll) in C#
[C#]
using System.IO;
using System.Reflection;
string path = Path.GetDirectoryName(
Assembly.GetAssembly(typeof(MyClass)).CodeBase);
File Attributes [C#]
Get file attributes in C#
[C#]
string filePath = @"c:\TEXT.txt";
// get file attributesFileAttributes fileAttributes = File.GetAttributes(filePath);
Set file attributes in C#
To set file attributes use static method File.SetAttributes. Parameter of the method is a bitwise combination of FileAttributes enumeration.
[C#]
// clear all file attributesFile.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 onlybool isReadOnly = ((File.GetAttributes(filePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
// check whether a file is hiddenbool isHidden = ((File.GetAttributes(filePath) & FileAttributes.Hidden) == FileAttributes.Hidden);
// check whether a file has archive attributebool isArchive = ((File.GetAttributes(filePath) & FileAttributes.Archive) == FileAttributes.Archive);
// check whether a file is system filebool 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 attributeFile.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.Hidden);
// delete/clear archive and read only attributesFile.SetAttributes(filePath, File.GetAttributes(filePath) & ~(FileAttributes.Archive |
FileAttributes.ReadOnly));
Get File Time [C#]
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.GetLastWriteTime 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.GetLastWriteTimeUtc.[C#]
// local timesDateTime creationTime = File.GetCreationTime(@"c:\TEXT.txt");
DateTime lastWriteTime = File.GetLastWriteTime(@"c:\ TEXT.txt");
DateTime lastAccessTime = File.GetLastAccessTime(@"c:\ TEXT.txt");
// UTC timesDateTime 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 timesDateTime creationTime = fileInfo.CreationTime;
DateTime lastWriteTime = fileInfo.LastWriteTime;
DateTime lastAccessTime = fileInfo.LastAccessTime;
// UTC timesDateTime 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#]
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 viewerSystem.Diagnostics.Process.Start(@"c:\image.jpg");
[C#]// open url in default web browserSystem.Diagnostics.Process.Start("http://www.csharp-examples.net");
[C#]// open PDF fileSystem.Diagnostics.Process.Start(@"c:\document.pdf");

0 comments: