Back to Devexpress

How to: Add Comment to File

officefileapi-15244-zip-compression-and-archive-api-examples-how-to-add-comment-to-file.md

latest1.9 KB
Original Source

How to: Add Comment to File

  • Sep 19, 2023

To add a comment to a file in an archive, do the following:

  1. Create a ZipArchive class instance.
  2. Call the ZipArchive.AddFile method for each selected file. This method returns an object of the ZipFileItem type.
  3. Assign a text string to the ZipItem.Comment property.
  4. Call the ZipArchive.Save method to create an archive and save it to a specified location.

This example adds a text comment to each zip item in the archive that indicates the person who is currently logged on.

View Example

csharp
using DevExpress.Compression;
//...

public void ArchiveWithComment() {
    string path = this.startupPath;
    using (ZipArchive archive = new ZipArchive()) {
        foreach (string file in System.IO.Directory.EnumerateFiles(path)) {
            ZipFileItem zipFI = archive.AddFile(file, "/");
            zipFI.Comment = "Archived by " + Environment.UserName;
        }
        archive.Save("ArchiveWithComment.zip");        
    }
}
vb
Imports DevExpress.Compression

Public Sub ArchiveWithComment()
    Dim path As String = Me.startupPath
    Using archive As New ZipArchive()
        For Each file As String In System.IO.Directory.EnumerateFiles(path)
            Dim zipFI As ZipFileItem = archive.AddFile(file, "/")
            zipFI.Comment = "Archived by " & Environment.UserName
        Next file
        archive.Save("ArchiveWithComment.zip")

    End Using
End Sub