如何从脚本的本地目录中包含Windows窗体的图像

时间:2015-04-10 21:45:00

标签: windows forms powershell directory root

我正在尝试开发一个lite应用程序来运行一组PowerShell脚本。我有一个小用户群,需要运行该应用程序。这个应用程序将有一些可以从一开始就运行的可显示选项(脚本)。用户需要能够单击按钮才能运行脚本。

我遇到的问题是当我不知道用户运行脚本的具体位置时,如何引用所需的文件。

我需要能够拥有一个背景图片,并将脚本存储在一个将包含在应用程序中的小文件夹中。有没有办法指导脚本在当前目录中查找文件?

这是我目前的代码:

#Credit: Portions of this code were provided at the following URL: 

http://blogs.technet.com/b/stephap/archive/2012/04/23/building-forms-with-powershell-part-1-the-form.aspx#pi47623=2

# LOAD WINDOWS FORMS
 # Load the Winforms assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  


 #Welcome Screen: 

 #code for welcome splash here. Haven't gotten that far yet. 


 # CREATE FORM
 $Form = New-Object System.Windows.Forms.Form    
 $Form.Size = New-Object System.Drawing.Size(500,400)  


 # background. This is where I need help. 
 $Image = [system.drawing.image]::FromFile("C:\Users\zstewart\Pictures\script runner.png") 

#this location is where my question arises. It won't work on another user's machine. 


 $Form.BackgroundImage = $Image
 $Form.BackgroundImageLayout = "Center"
 # None, Tile, Center, Stretch, Zoom

 #SET FORM TITLE
 $form.text = "ScriptRunner v.001"

#TEXT FIELD FOR PRINTING A RESULT
$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(40,60) 
$outputBox.Size = New-Object System.Drawing.Size(400,30) 
$outputBox.MultiLine = $True 
$outputBox.ScrollBars = "Vertical" #had horizontal here. Didn't work. 



 # BUTTON 
 # Create Button and set text and location
 $button = New-Object Windows.Forms.Button
 $button.Size = New-Object Drawing.Point 120,30
 $button.text = "Select Script"
 $button.Location = New-Object Drawing.Point 170, 100


 # INPUT HANDLER BUTTON - ON CLICK IN THIS CASE
 # Set up event handler to exit
 $button.add_click({
 Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
 $outputBox.text=$OpenFileDialog.filename
} #end function Get-FileName



 Get-FileName -initialDirectory "c:\fso"
 })


 # Create Button and set text and location
 $button2 = New-Object Windows.Forms.Button
 $button2.text = "Run Script"
 $button2.Size = New-Object Drawing.Point 120,30
 $button2.Location = New-Object Drawing.Point 170,140


 # INPUT HANDLER RUN BUTTON - RUN SCRIPT
 # close button - on click close app
 $button2.add_click({
 $form.Close() #need to add code to run some included scripts. 
 })


 # ADD CONTROLS TO FORM
 $form.controls.add($button)
 $form.controls.add($button2)
 $Form.Controls.Add($outputBox) 


 # DISPLAY DIALOG
 $form.ShowDialog()

我意识到这是一个非常非常脏的代码,但我仍然在努力学习。请原谅我的代码技巧。

1 个答案:

答案 0 :(得分:1)

在PS v3.0及更高版本中,您可以使用$PSScriptRoot automatic variable

引用脚本目录
$Image = [system.drawing.image]::FromFile("$PSScriptRoot\script runner.png") 

要使您的脚本PowerShell 2.0友好,您可以在脚本顶部执行以下操作:

if(-not (Get-Variable -Name 'PSScriptRoot' -Scope 'Script')) {
    $Script:PSScriptRoot = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
}