I need to unzip a file that is located in base directory, for example, sample.zip. I have made A sample application for doing so. I have 1 input parameter – destination directory. Here are code samples:
private void BInstall_Click(object sender, EventArgs e)
{
string currentdir = Directory.GetCurrentDirectory();//Gets current directory
string zip = currentdir + "\\" + "sample.zip";//Path to zip file
string outPath = TBoutputPath.Text;
exctract(zip ,outPath );
}
And here is the function that is supposed to extract the zip file:
void exctract(string name, string path)
{
string[] args = new string[2];
if (name.IndexOf(" ") != -1)
{
//we got a space in the path so wrap it in double qoutes
args[0] += "\"" + name + "\"";
}
else
{
args[0] += name;
}
if (path.IndexOf(" ") != -1)
{
//we got a space in the path so wrap it in double qoutes
args[1] += " " + "\"" + path + "\"";
}
else
{
args[1] +=path;
}
Shell32.Shell sc = new Shell32.Shell();
Shell32.Folder SrcFlder = sc.NameSpace(args[0]);
Shell32.Folder DestFlder = sc.NameSpace(args[1]);
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items , 20);
}
At DestFlder.CopyHere(items , 20); I get NullReferenceException, and I don’t know why, since the objects shouldn’t be null. It is DestFlder that is null; it seems that SrcFolder is initialized but DestFlder is not. The only difference I can find is that DestFlder doesn’t have a file extension following, but since it is a folder, it shouldn’t have one anyway.
Can anyone explain me what I did wrong and how to fix that?
The answer to this problem was rather… trivial, but as all of the simplest problems, next to impossible to think of.
The folder did not exist and therefor could not be referenced to. This code piece fixed that:
Thou DJ KRAZE did point to another problem with the script that could have it possibly get a run-time error eventually. Thank you for that!