Export (0) Print
Expand All

ZipArchiveEntry.LastWriteTime Property

.NET Framework 4.5

Gets or sets the last time the entry in the zip archive was changed.

Namespace:  System.IO.Compression
Assembly:  System.IO.Compression (in System.IO.Compression.dll)

public DateTimeOffset LastWriteTime { get; set; }

Property Value

Type: System.DateTimeOffset
The last time the entry in the zip archive was changed.

ExceptionCondition
NotSupportedException

The attempt to set this property failed, because the zip archive for the entry is in Read mode.

IOException

The archive mode is set to Create.

- or -

The archive mode is set to Update and the entry has been opened.

ArgumentOutOfRangeException

An attempt was made to set this property to a value that is either earlier than 1980 January 1 0:00:00 (midnight) or later than 2107 December 31 23:59:58 (one second before midnight).

When you create a new entry from an existing file by calling the CreateEntryFromFile method, the LastWriteTime property for the entry is automatically set to the last time the file was modified. When you create a new entry programmatically by calling the CreateEntry method, the LastWriteTime property for the entry is automatically set to the time of execution. If you modify the entry, you must explicitly set the LastWriteTime property if you want the value to reflect the time of the latest change.

When you set this property, the DateTimeOffset value is converted to a timestamp format that is specific to zip archives. This format supports a resolution of two seconds. The earliest permitted value is 1980 January 1 0:00:00 (midnight). The latest permitted value is 2107 December 31 23:59:58 (one second before midnight). If the value for the last write time is not valid, the property returns a default value of 1980 January 1 0:00:00 (midnight).

The following example shows how to open an entry in a zip archive, modify it, and set the LastWriteTime property to the current time.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\example\result.zip";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                ZipArchiveEntry entry = archive.GetEntry("ExistingFile.txt");
                using (StreamWriter writer = new StreamWriter(entry.Open()))
                {
                    writer.BaseStream.Seek(0, SeekOrigin.End);
                    writer.WriteLine("append line to file");
                }
                entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime;
            }
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft