You need to enable JavaScript to run this app. PowerShell GUI使用文本框输入创建文件夹及子文件夹问题
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell GUI使用文本框输入创建文件夹及子文件夹问题

PowerShell GUI 项目管理工具问题修复

现存问题说明

  • 路径字符串拼接错误
    PowerShell 双引号内直接插入对象属性不会自动解析,你写的"c:\Test_Ps1\$windowTextBox1.Text"最终生成的路径不符合预期,需要用$()包裹对象属性完成插值,正确写法为"C:\Test_Ps1\$($windowTextBox1.Text)"。
  • 缺少前置校验逻辑
    没有判断C:\Test_Ps1根目录是否存在、文本框输入是否为空、目标项目文件夹是否已经创建,任意条件不满足都会触发操作报错。
  • 复制文件夹参数缺失
    复制完整文件夹需要给Copy-Item加-Recurse参数,否则无法复制子目录和空文件夹。
  • 作用域适配问题
    按钮点击事件的脚本块中访问外部控件属性,部分PowerShell版本可能出现取值失败,建议将创建的项目路径存入脚本级变量复用。

修复后完整代码

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# 初始化基础路径,不存在则自动创建
$basePath = "C:\Test_Ps1"
if (-not (Test-Path $basePath)) {
    New-Item -Path $basePath -ItemType Directory | Out-Null
}
# 存储创建的项目根路径
$script:projectRoot = ""

$window = New-Object System.Windows.Forms.Form
$window.Width = 1300
$window.Height = 1000
$window.Text = "Project Management Dashboard"

$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Creator"
$Label1.AutoSize = $True
$Label1.ForeColor = "Black"
$window.Controls.Add($Label1)
 
$windowTextBox1 = New-Object System.Windows.Forms.TextBox
$windowTextBox1.Location = New-Object System.Drawing.Size(10,40)
$windowTextBox1.Size = New-Object System.Drawing.Size (300,500)
$window.Controls.Add($windowTextBox1)

$windowsbuttonquery = New-Object System.Windows.Forms.Button
$windowsbuttonquery.Location = New-Object System.Drawing.Point(400,800)
$windowsbuttonquery.Size = New-Object System.Drawing.Size(175,80)
$windowsbuttonquery.Text = "Project Package"
$windowsbuttonquery.ForeColor = "Black"
$window.Controls.Add($windowsbuttonquery)

$windowsbuttonquery.Add_Click({
    $inputName = $windowTextBox1.Text.Trim()
    # 校验输入不为空
    if ([string]::IsNullOrEmpty($inputName)) {
        [System.Windows.Forms.MessageBox]::Show("请先输入项目名称", "提示")
        return
    }
    $script:projectRoot = Join-Path $basePath $inputName
    # 不存在则创建文件夹
    if (-not (Test-Path $script:projectRoot)) {
        New-Item -Path $script:projectRoot -ItemType Directory | Out-Null
        [System.Windows.Forms.MessageBox]::Show("项目根文件夹创建成功", "提示")
    } else {
        [System.Windows.Forms.MessageBox]::Show("该项目文件夹已存在", "提示")
    }
})

$save = New-Object System.Windows.Forms.Button
$save.Location = New-Object System.Drawing.Point(700,800)
$save.Size = New-Object System.Drawing.Size(175,80)
$save.Text = "SAVE"
$save.ForeColor = "Black"
$window.Controls.Add($save)

$save.Add_Click({
    # 校验项目根路径已创建
    if ([string]::IsNullOrEmpty($script:projectRoot) -or -not (Test-Path $script:projectRoot)) {
        [System.Windows.Forms.MessageBox]::Show("请先点击Project Package创建项目根文件夹", "提示")
        return
    }
    $sourcePath = Join-Path $basePath "Z"
    if (-not (Test-Path $sourcePath)) {
        [System.Windows.Forms.MessageBox]::Show("源文件夹C:\Test_Ps1\Z不存在", "错误")
        return
    }
    # 复制文件夹,加-Recurse递归复制子内容
    Copy-Item -Path $sourcePath -Destination $script:projectRoot -Recurse -Force
    [System.Windows.Forms.MessageBox]::Show("文件保存成功", "提示")
    $window.Dispose()
})

[void]$window.ShowDialog()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 07:36:03