Rosa1995 Rosa1995 - 27 days ago 13
C# Question

How would I implement zipping all the files in a folder into my code?

So far I have a code that takes screenshots every 2 seconds and saves them to the a folder. I would like to add in a function for the program to take those screenshots every 2 hours and zip them up. I am currently working on trying to figure out how to set individual timers in my code, but after that I will need to add this.

I have been looking up and trying to learn how to do it, but my knowledge on c# code is extremely limited.

I tried adding the following into my code

using System.IO.Compression

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
string extractPath = @"c:\example\extract";
ZipFile.ExtractToDirectory(zipPath, extractPath);


but it comes back with "Using directive is unnecessary." for System.IO.Compression. Plus I can't figure out how my directory of C:\Intel\Logs\dsp would fit into each field ie.

c:\example\start
c:\example\result.zip
c:\example\extract


Can someone please help me figure out what I'm doing wrong and explain to me (in very blatant terms) how I can fix it?
Thank you so much!

P.S. Will adding in this part create the zip file and make it hidden? Also, which path do I put for that line then?

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
File.SetAttributes("c:\example\whichpath", FileAttributes.Hidden); //HERE
string extractPath = @"c:\example\extract";
ZipFile.ExtractToDirectory(zipPath, extractPath);

Answer Source

From here you need add what the comment recommends as below:

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

...

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";

ZipFile.CreateFromDirectory(startPath, zipPath);

// and hide it
File.SetAttributes(zipPath, File.GetAttributes(zipPath) | FileAttributes.Hidden);