I'm having some issues with the Shell32 CopyHere method.
First of all, I'm working from this page: http://www.codeproject.com/Tips/257193/Easily-zip-unzip-files-using-Windows-Shell32
I was hoping to rewrite something similar in C# just because I hardly ever work in VB.NET.
I'm passing an input directory i that has one text file in it as my test. At the bottom, just so I see that it's in fact grabbing my files, I write the count of input.Items() and I get 1, so I think it's seeing my directory with the one text file in it. However, while my code creates the empty zip file just fine, and at least from that console output appears to be grabbing my files, it doesn't actually copy anything into the zip file. No errors are thrown, it's just as if nothing happens.
static void Zip(string i, string o)
{
//Turn relative paths into full paths for Shell.NameSpace method.
string ifp = Path.GetFullPath(i);
string ofp = Path.GetFullPath(o);
//Validate parameters.
if (!(Directory.Exists(ifp)) || (Directory.GetFiles(ifp).Count() <= 0))
throw new Exception("Input directory " + i + " was invalid. " + i + " was either empty or doesn't exist.");
string ext = ofp.Substring(ofp.Length - 4, 4);
if (ext != ".zip")
throw new Exception("Output zip directory " + o + " was invalid. File extension was " + ext + " when it should be .zip.");
if (File.Exists(ofp))
throw new Exception("Output zip directory " + o + " was invalid. Zip file already exists.");
//The following data represents an empty zip file.
byte[] startBuffer = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//Create empty zip file.
File.WriteAllBytes(ofp, startBuffer);
if (!File.Exists(ofp))
throw new Exception("Output zip directory " + o + " was unable to be created.");
Shell sc = new Shell();
//Folder which contains the files you want to zip.
Folder input = sc.NameSpace(ifp);
//Empty zip file as folder.
Folder output = sc.NameSpace(ofp);
//Copy the files into the empty zip file.
output.CopyHere(input.Items(), 4);
Console.WriteLine(input.Items().Count);
}
Is there something wrong with how I'm using the Shell32 methods?
EDIT:
Sorry for the slow response, I'm trying to work with some of the code DJ Kraze put up.
To clarify some things, unfortunately I can't use a third party tool and I'm on .NET 4.0. I'm going to try taking DJ's advice and see if I can get either the VB version working or the C# that he kindly posted.
Thanks for the help everyone.