我想在powershell中创建一个zip文件,将项目添加到zip文件,然后在创建zip文件后立即将该zip文件的压缩内容作为字节,在同一个scipt中.问题是,zip应用程序似乎没有将其内容写入文件系统.如何关闭/刷新zip应用程序,以便下一个powershell语句可以访问新创建的zip文件?
例:
new-item -type File test.zip -force
$zip = ( get-item test.zip ).fullname
$app = new-object -com shell.application
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname )
dir test.zip # <---- Empty test.zip file
Get-Content -Encoding byte $zip | echo # <-- nothing echoed
Run Code Online (Sandbox Code Playgroud)
"dir test.zip"显示没有内容的zip文件,因此Get-Content不返回任何内容.
请注意,这似乎是copyhere操作的异步行为的问题.如果我在copyhere行之后睡觉,zip文件将被填充.但是,我不知道必须睡多久,也不想延迟处理.
非常感谢提前!
我一直在尝试让 ReadDirectoryChangesW 监视子树的文件更改,但我发现得到的结果不一致。以下是一个说明问题的独立测试用例。当我运行它时,它有时会产生:
A : Created
C : Updated
A : Deleted
Run Code Online (Sandbox Code Playgroud)
另一次它可能会产生:
A : Created
B : Updated
C : Updated
A : Deleted
Run Code Online (Sandbox Code Playgroud)
我创建了一个巨大的缓冲区,并且正在更改的文件数量非常小(3 个文件)。
代码:
import os, sys, time, threading
import win32file, win32event, win32con, pywintypes
class ChangeFiles ( threading.Thread ) :
def run( self ) :
files = [ 'A', 'B', 'C' ]
time.sleep( 1 )
for path in files : f = open( path, 'w' ); f.write( 'mooo' ); f.close()
time.sleep( 0.5 )
for path in …Run Code Online (Sandbox Code Playgroud) 我正在使用t4(文本模板)生成电子邮件,并希望为我的模板提供一个通用的基类。为此,我创建了一个电子邮件基础模板,并使我的所有电子邮件模板都继承自该模板。像这样:
基本模板:
<#@ template language="C#" #>
Run Code Online (Sandbox Code Playgroud)
派生模板:
<#@ template language="C#" inherits="BaseTemplate" #>
<#@ parameter name="Param" type="System.String" #>
Template! Param=<#= Param #>
Run Code Online (Sandbox Code Playgroud)
注意派生模板中的参数。如果存在,它将导致模板具有Initialize方法。并且,由于派生的模板派生自基本模板,因此派生模板中的Initialize方法的声明使用关键字“ override”。但是,基本模板上没有Initialize方法。这会导致错误:
'Template.Initialize()': no suitable method found to override
Run Code Online (Sandbox Code Playgroud)
我可以通过在基本模板中声明一个虚拟参数来解决此问题:
<#@ template language="C#" #>
<#@ parameter name="DummyParam" type="System.String" #>
Run Code Online (Sandbox Code Playgroud)
这将导致在基础模板中生成一个Initialize方法,从中可以继承派生模板。
我的问题是,我缺少什么吗?必须添加一个虚拟参数来使编译器适应,这似乎使我做错了。