简体   繁体   中英

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. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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