使用 shell 解压缩 zip 文件时出现参数错误
我需要解压缩位于基本目录中的文件,例如,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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:
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!