You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

双骰子PowerShell脚本转Exe后报错:无法对空值表达式调用方法

问题分析与修复方案

核心错误原因

  1. 未定义变量调用:脚本中$Die.Add_Click({ Roll })这一行的$Die变量从未初始化,属于空值,调用Add_Click方法直接触发"无法在空值表达式上调用方法"的报错。
  2. EXE运行路径问题:Roll函数里的.\\Dice.exe是相对路径,ps2exe生成的EXE运行时,当前工作目录可能并非脚本原目录,会导致找不到自身文件。
  3. 硬编码图片路径:f:\psscripts\Die1.png是固定绝对路径,若EXE移动到其他环境,会因找不到图片文件引发新错误。

修复后的完整脚本

<# 
.NAME
    Random Dice Roller
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

# 获取当前脚本/EXE的目录,解决路径适配问题
$currentDir = if ($MyInvocation.MyCommand.Path) {
    Split-Path -Path $MyInvocation.MyCommand.Path -Parent
} else {
    $PWD.Path
}

# 构建图片的相对路径(假设Die1.png与脚本/EXE在同一目录)
$imagePath = Join-Path -Path $currentDir -ChildPath "Die1.png"
$LabelImage = [system.drawing.image]::FromFile($imagePath)

$Display1 = 1,2,3,4,5,6 | Get-Random
$Display2 = 1,2,3,4,5,6 | Get-Random

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(400,300)
$Form.text                       = "Roll The Dice"
$Form.TopMost                    = $false
$Form.StartPosition              = "CenterScreen"

$Die1                             = New-Object system.Windows.Forms.Label
$Die1.Text                        = "$Display1"
$Die1.AutoSize                    = $false
$Die1.width                       = 200
$Die1.height                      = 200
$Die1.location                    = New-Object System.Drawing.Point(1,1)
$Die1.Font                        = New-Object System.Drawing.Font('Microsoft Sans Serif',150,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Die1.ForeColor                   = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$Die1.BackgroundImage = $LabelImage
# 给Die1添加点击事件(原脚本的$Die应为Die1或Die2)
$Die1.Add_Click({ Roll })

$Die2                             = New-Object system.Windows.Forms.Label
$Die2.Text                        = "$Display2"
$Die2.AutoSize                    = $false
$Die2.width                       = 200
$Die2.height                      = 200
$Die2.location                    = New-Object System.Drawing.Point(200,1)
$Die2.Font                        = New-Object System.Drawing.Font('Microsoft Sans Serif',150,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Die2.ForeColor                   = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$Die2.BackgroundImage = $LabelImage
# 给Die2添加点击事件
$Die2.Add_Click({ Roll })

$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(175,220)
$Button1.Size = New-Object System.Drawing.Size(80,50)
$Button1.Text = "Roll Me"
$Button1.Add_Click({Roll})

$Form.controls.AddRange(@($Die1,$Die2,$Button1))

#region Logic 
function Roll{
    $Form.Close()
    $Form.Dispose()
    # 用完整路径启动EXE,避免相对路径问题
    Start-Process -FilePath (Join-Path -Path $currentDir -ChildPath "Dice.exe")
}
#endregion

[void]$Form.ShowDialog()

修复要点说明

  • 修正未定义变量:删除原脚本中无效的$Die.Add_Click({ Roll })行,按需给$Die1和$Die2添加点击事件。
  • 动态获取运行目录:通过$MyInvocation.MyCommand.Path获取EXE实际路径,拼接图片和自身路径,解决不同环境下的路径适配问题。
  • 优化重启逻辑:使用Start-Process结合完整路径启动EXE,避免相对路径导致的文件找不到问题。

内容的提问来源于stack exchange,提问作者PCMedicJAX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 14:54:19