使用shell反编译zip文件时出现参数错误

本文关键字:参数 错误 文件 shell 编译 zip 使用 | 更新日期: 2023-09-27 18:25:15

我需要解压缩一个位于基本目录中的文件,例如sample.zip。我为此制作了一个示例应用程序。我有一个输入参数-目标目录。以下是代码示例:

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

下面是应该提取zip文件的函数:

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

DestFlder.CopyHere(items , 20);中,我得到了NullReferenceException,我不知道为什么,因为对象不应该为null。DestFlder为空;似乎SrcFolder已初始化,但DestFlder未初始化。我能找到的唯一区别是DestFlder后面没有文件扩展名,但由于它是一个文件夹,所以无论如何都不应该有。

有人能解释我做错了什么以及如何解决吗?

使用shell反编译zip文件时出现参数错误

这个问题的答案是。。。琐碎,但作为所有最简单的问题,几乎不可能想到。

文件夹不存在,因此无法引用。此代码段修复了以下问题:

        if (!Directory.Exists(args[1]))
            Directory.CreateDirectory(args[1]);

你DJ KRAZE确实指出了脚本的另一个问题,这个问题可能会导致它最终出现运行时错误。谢谢你!