Hi landomarossi,
Use Directory.GetFiles to retrive all the files bay passing the Path, SearchPattern and the Search Option.
Path - This is the Directory path.
SearchPattern - The Pattern you wan tot search.
SearchOption - Specifies whether you want to search all Directories.
Check the below example.
HTML
<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="DownloadFiles" />
Namespaces
C#
using System.IO;
using Ionic.Zip;
VB.Net
Imports System.IO
Imports Ionic.Zip
Code
C#
protected void DownloadFiles(object sender, EventArgs e)
{
string monthName = DateTime.Now.AddMonths(-1).ToString("MMM");
string directoryName = DateTime.Now.AddMonths(-1).ToString("MMM_yyyy");
string[] files = Directory.GetFiles(Server.MapPath("~/Files"), monthName + "_*.pdf", SearchOption.AllDirectories);
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName(directoryName);
foreach (string filePath in files)
{
zip.AddFile(filePath, directoryName);
}
zip.Save(Server.MapPath("~/" + directoryName + ".zip"));
}
}
VB.Net
Protected Sub DownloadFiles(ByVal sender As Object, ByVal e As EventArgs)
Dim monthName As String = DateTime.Now.AddMonths(-1).ToString("MMM")
Dim directoryName As String = DateTime.Now.AddMonths(-1).ToString("MMM_yyyy")
Dim files As String() = Directory.GetFiles(Server.MapPath("~/Files"), monthName & "_*.pdf", SearchOption.AllDirectories)
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectoryByName(directoryName)
For Each filePath As String In files
zip.AddFile(filePath, directoryName)
Next
zip.Save(Server.MapPath("~/" + directoryName + ".zip"))
End Using
End Sub
Screenshots
Files inside the Zip Folder