Delete files with a particular extension (C#)

You can delete files using a simple batch file. That is,

cd C:\test
del *.txt

This code deletes all text files in C:\test (but not recursively).

Tips: "*" means an arbitrary name.

C# Code

var files = Directory.GetFiles( "C:\test", "*.txt" );
foreach ( var file in files )
{
    File.Delete( file );
}

Of course, you can use a Parallel.ForEach loop. Deleting files parallelly must be processed after listing files.

var files = Directory.GetFiles( "C:\test", "*.txt" );
Parallel.ForEach( files, file =>
    {
        File.Delete( file );
    }
);

Using File Class, "*" means an arbitrary name.

広告

広告

広告

姓名判断

漢字辞典

色辞典

数の辞典

高校化学専門サイト

Python解説サイト

© Irohabook