简体   繁体   English

如何在C#中压缩最后修改的文件

[英]How to compress last modified file in C#

I have lots of file which have same name in a directory. 我在目录中有很多同名文件。 I want to compress (zip, etc) the last modified file in MyDirectory. 我想在MyDirectory中压缩(压缩等)最后修改的文件。 Can you help me please? 你能帮我吗?

I can compress a file but not last modified file: 我可以压缩文件,但不能压缩最后修改的文件:

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");
}

To get last modified file: 要获取最后修改的文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM