Reputation: 11146
I have an application that should unzip and install some other application, i get new Zip file with installation image via network and then should unzip it and install. All file except the part that Zip file comes with different name each time because of version change and all my C:\ has a lot of .zip files with different versions. Msi file is not overwritten by Zip i'm using but i'm more than fine with just deleting it prior to unzipping.
I want to delete all .msi and .zip files on C:. How can i do that via C#?
thanks...
Upvotes: 32
Views: 47665
Reputation:
Directory.GetFiles(@"C:\", "*.pdf").ToList().ForEach(f => File.Delete(f));
Upvotes: 2
Reputation: 2733
foreach (string file in Directory.GetFiles("C:\\myDir", "*.zip").Where(item => item.EndsWith(".zip")))
{
File.Delete(file);
}
you can do it with more pattern by changing the GetFiles Algo
public FileInfo[] GetFiles(DirectoryInfo dir, string searchPatterns, params char[] separator)
{
ArrayList files = new ArrayList();
string[] patterns = searchPatterns.Split(separator);
foreach (string pattern in patterns)
{
if (pattern.Length != 0)
{
files.AddRange(dir.GetFiles(pattern).Where(item => item.EndsWith(pattern));
}
}
return (FileInfo[])files.ToArray(typeof(FileInfo));
}
Upvotes: 17
Reputation: 57593
You could try something like this:
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] files = di.GetFiles("*.msi")
.Where(p => p.Extension == ".msi").ToArray();
foreach (FileInfo file in files)
try
{
file.Attributes = FileAttributes.Normal;
File.Delete(file.FullName);
}
catch { }
Note that I first try to set attributes to "normal", because File.Delete()
fails if file is read-only...
Note the use of GetFiles()
: see this link for details.
EDITED:
If you need to get more than one extension you could use this:
public List<FileInfo> GetFiles(string path, params string[] extensions)
{
List<FileInfo> list = new List<FileInfo>();
foreach (string ext in extensions)
list.AddRange(new DirectoryInfo(path).GetFiles("*" + ext).Where(p =>
p.Extension.Equals(ext,StringComparison.CurrentCultureIgnoreCase))
.ToArray());
return list;
}
so you can change part of my answer to
List<FileInfo> files = GetFiles(@"C:\", ".msi", ".zip");
Upvotes: 65
Reputation: 1631
You can simply do this :
Directory.EnumerateFiles("C:\\MyDirectory", "*.ext").ToList().ForEach(x => File.Delete(x));
Don't forget to include System.IO
Upvotes: 6
Reputation: 63105
var files = Directory.GetFiles(@"C:\", "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".msi") || s.EndsWith(".zip"));
foreach (string file in files)
{
File.Delete(file);
}
Upvotes: 4
Reputation: 4683
You can list all file with specific extension and delete all
var list = System.IO.Directory.GetFiles("*.zip");
foreach (var item in list)
{
System.IO.File.Delete(item);
}
Upvotes: 1
Reputation: 121881
The C# System.IO.Directory object lets you find files (e.g. *.zip) and System.IO.File lets you delete them:
http://msdn.microsoft.com/en-us/library/wz42302f.aspx
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
Additionally, the .Net 3.5++ file "ZipPackage" might let you do some/all of what you want directly inside the .zip file (without necessarily extracting anything first):
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage%28v=VS.90%29.aspx
Upvotes: 2