使用 shell 解压缩 zip 文件时出现参数错误

发布于 2025-01-07 16:18:38 字数 1397 浏览 1 评论 0原文

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

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,但我不知道为什么,因为对象不应该为空。 DestFlder 为 null;似乎 SrcFolder 已初始化,但 DestFlder 未初始化。我能发现的唯一区别是 DestFlder 后面没有文件扩展名,但由于它是一个文件夹,所以无论如何也不应该有文件扩展名。

谁能解释一下我做错了什么以及如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2025-01-14 16:18:38

这个问题的答案相当……微不足道,但作为所有最简单的问题,几乎不可能想到。

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

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

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

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:

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文