我一直在寻找所有的互联网,但一直无法找到答案。也许我在寻找错误的东西。我正在尝试读取文本文件并让它更新GUI菜单栏。我所拥有的是:
$folder = Get-ChildItem $FilePath
Foreach($file in $folder){
$(Filename)= new-system System.Windows.Forms.ToolStripMenuItem
$(filename).Name = "$(filename)"
$(filename).Size = new-object System.Drawing.Size(35, 20)
$(filename).Text = "&(filename)"
$(foldername).item.add($(Name of file))
$var = get-content c:\test\$file
foreach($line in $var){
$A = $line.split("^")
$(Name of object) = new-system System.Windows.Forms.ToolStripMenuItem
$(Name of object).Name = "$a[0]"
$(Name of object).Size = new-object System.Drawing.Size(35, 20)
$(Name of object).Text = "&$a[1]"
$(FileName).item.add($(Name of object))
如何让它为文件及其中的每一行创建不同的变量名?我希望我的要求足够清楚。每个文件不会有超过20行读取。
答案 0 :(得分:0)
$var = get-content c:\test\links.txt
foreach($line in $var){
$A = $line.split("^")
$variablename = "$"+ "var0"+ $a[0]
$createObject = "$Variablename = new-object system.windows.forms.toolstripmenuitem"
$settext = "$variablename.text = $a[0]"
$setsize = "$variablename.size = new-object System.drawing.size(35,20)"
$addmenu = "$ToolStripMenuItem.item.add($variablename)"
Invoke-expression $createObject
Invoke-expression $settext
Invoke-expression $setsize
Invoke-expression $addmenu
}