【发布时间】:2017-08-12 01:13:24
【问题描述】:
我想使用 PowerShell 和 C# 将下载文件夹中的 zip 文件解压缩到桌面。
我需要它与 Windows 7、8 和 10 一起使用。
我正在尝试使用这些 PowerShell 命令
- 提取https://stackoverflow.com/a/36472063/6806643
- 覆盖https://stackoverflow.com/a/5711383/6806643
- 链https://superuser.com/a/612413/740888
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("zip file path")
foreach ($item in $zip.items()) {
$shell.Namespace("unzip destination path").CopyHere($item)
}
并通过 C# Process.Start() 运行它
Process.Start("powershell.exe",
"timeout 3; "
+ "$shell = New-Object -ComObject shell.application; "
+ "$zip = $shell.NameSpace(\"C:\\Users\\Matt\\Downloads\\MyFile.zip\"); "
+ "foreach ($item in $zip.items()) {$shell.Namespace(\"C:\\Users\\Matt\\Desktop\\\").CopyHere($item, 0x14)}"
);
问题
PowerShell 启动但无法提取,并在我读取错误之前关闭。
但是,如果我将这些链接的命令复制粘贴到没有 C# 的 PowerShell 中,它就可以工作。
$shell = New-Object -ComObject shell.application; $zip = $shell.NameSpace('C:\Users\Matt\Downloads\MyFile.zip'); foreach ($item in $zip.items()) {$shell.Namespace('C:\Users\Matt\Desktop\').CopyHere($item, 0x14)}
这可行,但仅适用于 PowerShell 5。
Process.Start("powershell.exe",
"timeout 3; Expand-Archive 'C:\\Users\\Matt\\Downloads\\MyFile.zip' -Force -DestinationPath 'C:\\Users\\Matt\\Desktop\\'"
);
【问题讨论】:
标签: c# powershell