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