0

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);
Rosa1995
  • 63
  • 1
  • 7
  • There's an example (in both C# and VB.Net) in the [System.IO.Compression.ZipFile.CreateFromDirectory() documentation](https://msdn.microsoft.com/en-us/library/hh485721(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2) at MSDN. Doesn't it show you exactly what you need? And why are you zipping them and then immediately unzipping them? You can remove 100% of your code and end up with the same results without the clutter of a .zip file in the folder that you're not using. – Ken White Aug 24 '17 at 00:51
  • I was honestly just going off of [this](https://stackoverflow.com/questions/39732407/create-zip-file-from-all-files-in-folder). I have no idea what I'm doing. I tried to read that link you sent me a few times actually and I just don't understand it so I thought I might post to the forums for help. – Rosa1995 Aug 24 '17 at 01:00
  • There's nothing to understand. The code sample is a very simple, very clear example of using the function. (You **are** looking at the example code under the heading **Example**, right?) If you can't read that, how are you going to read an answer here? – Ken White Aug 24 '17 at 01:04
  • Oh, okay. I just had a minor error with the organization of my lines, sorry. I've fixed it now. The only thing I don't understand is how my directory of C:\Intel\Logs\dsp would fit into each field (ie. `c:\example\start` `c:\example\result.zip` `c:\example\extract`). – Rosa1995 Aug 24 '17 at 01:10
  • Also, why does the example include `string extractPath = @"c:\example\extract";` and `ZipFile.ExtractToDirectory(zipPath, extractPath);` if I'm not trying to extract the zip file right after I zipped it in the first place? – Rosa1995 Aug 24 '17 at 01:11
  • So they can link the same example from both function's documentation. Clearly you wouldn't use **extract** when you're trying to **create**. Do you understand the meaning of the word *extract* and what it does? – Ken White Aug 24 '17 at 01:12
  • I understand the meaning of the word _extract_ and what it does. That's why I was so confused. I guess I just didn't read correctly. So I would exclude that part from my code and just write it like [this](http://csharppad.com/gist/54489c181b95509e1c1be3c173fc6538)? – Rosa1995 Aug 24 '17 at 01:19
  • The paragraph of text above the example explains that it *shows how to create and extract a zip archive by using the ZipFile class. It compresses the contents of a folder into a zip archive, and then **extracts that content to a new folder***. That's what made me think you might not understand extract. (Re-reading my last comment sounds somewhat rude, and I apologize for the way it sounds. Certain phrasing in your question and comments sound as if English isn't your first language, which is why I asked. The tone was unintended; again, I apologize.) – Ken White Aug 24 '17 at 01:42
  • Oh, I understand now. It's okay. I'm actually recovering from a brain tumor so my speech and everything isn't the best, I apologize. – Rosa1995 Aug 24 '17 at 01:44
  • Congratulations! Recovering is so much better than the alternative! :-) No apologies necessary. – Ken White Aug 24 '17 at 01:48
  • Thank you! That's very kind of you. :) So, getting back to the extracting thing. Would I just exclude that part from my code and write it like [this](http://csharppad.com/gist/54489c181b95509e1c1be3c173fc6538) or...? – Rosa1995 Aug 24 '17 at 01:50
  • You can leave out the last two lines (the declaration related to `extractPath` and the call to `ExtractDirectory`). I'm afraid I can't go to an off-site location to review your code; that's a violation of the site guidelines. All discussion related to a question has to be here, for the benefit of future readers. You can try the code yourself (after removing those two lines - I'd also comment out the one that hides the file temporarily so you can easily examine the contents during testing) to see if it does what you need. – Ken White Aug 24 '17 at 01:55
  • It's a violation? Huh, I never knew that. Thank you for letting me know. I only did it because the code function in the comments doesn't allow for multiple lines like it does in the original question area. I'll try here again though just to see. `string startPath = @"c:\example\start"; string zipPath = @"c:\example\result.zip"; ZipFile.CreateFromDirectory(startPath, zipPath);` – Rosa1995 Aug 24 '17 at 02:01
  • Nope. Just can't get it to line break. – Rosa1995 Aug 24 '17 at 02:01
  • Also, to test it I'll need to figure out how my directory of C:\\Intel\\Logs\\dsp would fit into each field (ie. `c:\example\start` `c:\example\result.zip`). – Rosa1995 Aug 24 '17 at 02:03
  • Um... `string StartPath` would contain your directory, and `string ZipPath` would contain the path and filename of your zip file. – Ken White Aug 24 '17 at 02:04
  • I figured, but whenever I put `string zipPath = @"c:\Intel\Logs\zip"` It creates an error saying that `c:\Intel\Logs\zip` already exists. Also, I just realized, will this create a new zip file every 2 hours and not just overwrite the existing one? Like if I were to check the location after 6 hours there'd be 3 separate zip files, right? – Rosa1995 Aug 24 '17 at 02:09
  • `zipPath` should include the **path and filename** of the zip file. If the file already exists, you've already used that path and filename. You can't have more than one file in a folder with the same exact name. Use a different *filename*, or rename the existing file first. This is basic computer use. You should probably learn how to use your operating system before you write code that moves files around. – Ken White Aug 24 '17 at 02:12
  • I didn't mean it like that. There's not a file like with that name already in existence. That's why I don't get why it's calling this error. And about the multiple files thing I asked. I meant is there a way to have it create a different name for each zip file so they don't create errors (ie. zip1, zip2, etc.)? I know that early on in my code it creates new names for each new screenshots for this same reason I just don't know how to take that and apply it to this part of the code. I hope that makes sense. – Rosa1995 Aug 24 '17 at 02:16
  • The file is probably hidden (which is what the line that sets the attributes does, and why I suggested commenting it out during testing). This is going on far too long, I'm afraid. This really isn't a tutorial site. You're now changing it into a totally different discussion and topic, which should be asked in a new question. Comments are for requesting clarification regarding a question or answer. They're not for entire new questions about other topics or tutorials about how to use your OS. (And if you have existing code that generates sequential filenames, it's a simple change to use here.) – Ken White Aug 24 '17 at 02:20
  • It's starting to appear like you're just copy/pasting code without making any effort to understand how it works or what it does, which (clearly) isn't going to get you very far. Learn what the code is doing, and you'll have a much easier time changing it to work slightly differently when needed. Copy/paste coding is dangerous, especially when you're moving and deleting files. – Ken White Aug 24 '17 at 02:23
  • Sorry that it seems that way. All I really had a question about was how to make it so that the code creates new file names for each new zip file like the screenshot part of my code, but I understand if you don't want to help me with that. You've helped a ton already and I thank you for that. – Rosa1995 Aug 24 '17 at 02:26
  • The question you've posted here (at the very top of this page) has nothing to do with sequentially naming files. It asks about zipping files. If you now have a question about generating sequential filenames, first search here for an existing question about doing that in C#, and if you can't find one then post a new question asking about that topic. We can't continue to ignore the *Please avoid extended discussions in comments* while you ask totally new questions and learn how your operating system works, I'm afraid. The [tour] and [help] may help understand how this site works. – Ken White Aug 24 '17 at 02:29
  • Okay, thank you. I'll go do that right now. – Rosa1995 Aug 24 '17 at 02:30

1 Answers1

1

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);
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • Isn't that what I have currently though? (Well besides the file attribs part) Or am I missing something? (I just realized this may sound rude, but I assure you I am actually just confused. Thanks for the help.) – Rosa1995 Aug 24 '17 at 01:03
  • @Rosa1995: You are missing `;` at the end of `using` line and missing another `using`. – Xaqron Aug 24 '17 at 01:05
  • Oh! I see that now. Thank you so much! I can't believe I missed such tiny problems. – Rosa1995 Aug 24 '17 at 01:12
  • I'm also confused as to why I would include `string extractPath = @"c:\example\extract";` and `ZipFile.ExtractToDirectory(zipPath, extractPath);` if I'm not trying to extract the zip file right after I zipped it in the first place. Would I just exclude it and write it like [this](http://csharppad.com/gist/54489c181b95509e1c1be3c173fc6538)? – Rosa1995 Aug 24 '17 at 01:38
  • Never mind. Ken White helped me figure it out. Thanks! :) – Rosa1995 Aug 24 '17 at 02:28
  • Sorry I didn't see your last comment. Yes you can omit that part (answer updated). – Xaqron Aug 24 '17 at 02:31
  • So I still need `ZipFile.ExtractToDirectory(zipPath, extractPath);`? – Rosa1995 Aug 24 '17 at 02:45
  • No. You need it if you want to extract the zip file. – Xaqron Aug 24 '17 at 02:47
  • Oh, like in the future? It doesn't make the code automatically extract the zip file? – Rosa1995 Aug 24 '17 at 03:00