如何在C#中压缩上次修改的文件

时间:2014-03-12 19:53:13

标签: c# .net compression last-modified

我有很多在目录中具有相同名称的文件。我想在MyDirectory中压缩(zip等)最后修改的文件。你能帮帮我吗?

我可以压缩文件但不能压缩上次修改的文件:

private void button1_Click(object sender, EventArgs e)
{
    FileStream sourceFile = File.OpenRead(@"C:\MyDirectory");
    FileStream destFile = File.Create(@"C:\MyDirectory.zip");
    GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
    try
    {
        int theByte = sourceFile.ReadByte();
        while (theByte != -1)
        {
            compStream.WriteByte((byte)theByte);
            theByte = sourceFile.ReadByte();
        }
    }
    finally
    {
        compStream.Dispose();
    }
    MessageBox.Show("file is compressed successfully");
}

1 个答案:

答案 0 :(得分:1)

获取上次修改的文件:

string lastModified = Directory.EnumerateFiles(path)
                               .OrderBy(f => File.GetLastWriteTime(f))
                               .Last();