我需要上传一个文件并从地址中获取文件名
I need to upload a file and take the file name from the address
下面的代码将打开一个初始 window,您需要在其中输入要上传的文件数量,然后在下一个 window 您 select 要上传的文件上传。在这里你会看到你上传的文件的完整路径,但我只需要 selected 的文件名,而不是显示的完整路径。 System.Windows.Forms.TextBox, Text: C:\Users\Gourav Bid\Desktop\pwd.txt
。我只需要取出pwd.txt
.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
###### Initial Form Design ######
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "File Upload"
$objForm.Size = New-Object System.Drawing.Size(350,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
###### Label & TextBox Design ######
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Point(40,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter The Number Of Artefacts File Required :"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Point(40,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
###### Operation Buttons & Cancel Button Design ######
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,80)
$OKButton.Size = New-Object System.Drawing.Size(70,20)
$OKButton.Text = "OK"
$OKButton.Add_Click{(Button), $objForm.Close()}
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(120,80)
$CancelButton.Size = New-Object System.Drawing.Size(70,20)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
function Button {
[int]$artefacts = $objTextBox.Text
$d = 100 + $artefacts*30
$OKLoc = $d-70
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Size = New-Object System.Drawing.Size(400,$d)
$Form1.Text = "Select Artefacts Files"
$Form1.StartPosition = "CenterScreen"
$Form1.AutoScroll = $True
$Form1.KeyPreview = $True
############################################## Buttons ##############################################
$OKButton1 = New-Object System.Windows.Forms.Button
$OKButton1.Location = New-Object System.Drawing.Size(20,$OKLoc)
$OKButton1.Size = New-Object System.Drawing.Size(75,23)
$OKButton1.Text = "OK"
$OKButton1.Add_Click({$x=$objTextBox.Text;$Form1.Close();{split};Write-Host "$arte1"})
$Form1.Controls.Add($OKButton1)
$CancelButton1 = New-Object System.Windows.Forms.Button
$CancelButton1.Location = New-Object System.Drawing.Size(150,$OKLoc)
$CancelButton1.Size = New-Object System.Drawing.Size(75,23)
$CancelButton1.Text = "Cancel"
$CancelButton1.Add_Click({$Form1.Close()})
$Form1.Controls.Add($CancelButton1)
############################################## Buttons ##############################################
if ($artefacts -eq 1) {
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(15,20)
$Label.Size = New-Object System.Drawing.Size(50,20)
$Label.Text = "File 1"
$Form1.Controls.Add($Label)
$arte = New-Object System.Windows.Forms.TextBox
$arte.Location = New-Object System.Drawing.Size(80,15)
$arte.Size = New-Object System.Drawing.Size(280,20)
$Form1.Controls.Add($arte)
}
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(270,$OKLoc)
$Button.Size = New-Object System.Drawing.Size(75,23)
$Button.Text = "Browse"
$Button.Add_Click({Button})
#Write-Host $test
$Form1.Controls.Add($Button)
#####################################################Function Button#####################################################
function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [string]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect) {
Add-Type -AssemblyName System.Windows.Forms
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Title = $WindowTitle
if (![string]::IsNullOrWhiteSpace($InitialDirectory)) {
$openFileDialog.InitialDirectory = $InitialDirectory
}
$openFileDialog.Filter = $Filter
if ($AllowMultiSelect) {
$openFileDialog.MultiSelect = $true
}
$openFileDialog.ShowHelp = $true
# Without this line the ShowDialog() function may hang depending on system configuration and running from console vs. ISE.
$openFileDialog.ShowDialog() > $null
if ($AllowMultiSelect) {
return $openFileDialog.Filenames
} else {
return $openFileDialog.Filename
}
}
function Button {
for ($a=0;$a -lt $artefacts;$a++) {
$b = $a+1
$arte[$a].Text = Read-OpenFileDialog -WindowTitle "Select File $b" -InitialDirectory "C:\" -Filter "ALL files (*.*)|*.*"
}
Write-Host "$arte"
$arte1=$arte
}
function Split {
$arte1.split('\')[1]#.split(',')[0]
Write-Host "$arte1"
}
#####################################################Function Button
if ($artefacts -gt 1) {
$b = 20
$c = 15
for ($a=0;$a -lt $artefacts;$a++) {
$d = $a + 1
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(15,$b)
$Label.Size = New-Object System.Drawing.Size(50,20)
$Label.Text = "File $d"
$Form1.Controls.Add($Label)
[Array]$arte += New-Object System.Windows.Forms.TextBox
$arte[$a].Location = New-Object System.Drawing.Size(80,$c)
$arte[$a].Size = New-Object System.Drawing.Size(280,20)
$Form1.Controls.Add($arte[$a])
$b = $b+30
$c = $c+30
}
}
$Form1.Add_Shown({$Form1.Activate()})
[void] $Form1.ShowDialog()
}
$objForm.Topmost = $false
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
如果只需要文件名,可以使用Split-Path
:
Split-Path "C:\Users\Gourav Bid\Desktop\pwd.txt" -Leaf
产出
pwd.txt
在你的例子中,你可以替换
Write-Host "$arte"
和
$arte | % { Write-Host (Split-Path $_.Text -Leaf) }
它将迭代 $arte
,并从 .Text
属性 中提取文件名。
你得到的输出是整个 TextBox
对象,你需要指定你想要哪个 属性,在本例中是 .Text
.
下面的代码将打开一个初始 window,您需要在其中输入要上传的文件数量,然后在下一个 window 您 select 要上传的文件上传。在这里你会看到你上传的文件的完整路径,但我只需要 selected 的文件名,而不是显示的完整路径。 System.Windows.Forms.TextBox, Text: C:\Users\Gourav Bid\Desktop\pwd.txt
。我只需要取出pwd.txt
.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
###### Initial Form Design ######
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "File Upload"
$objForm.Size = New-Object System.Drawing.Size(350,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
###### Label & TextBox Design ######
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Point(40,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter The Number Of Artefacts File Required :"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Point(40,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
###### Operation Buttons & Cancel Button Design ######
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,80)
$OKButton.Size = New-Object System.Drawing.Size(70,20)
$OKButton.Text = "OK"
$OKButton.Add_Click{(Button), $objForm.Close()}
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(120,80)
$CancelButton.Size = New-Object System.Drawing.Size(70,20)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
function Button {
[int]$artefacts = $objTextBox.Text
$d = 100 + $artefacts*30
$OKLoc = $d-70
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Size = New-Object System.Drawing.Size(400,$d)
$Form1.Text = "Select Artefacts Files"
$Form1.StartPosition = "CenterScreen"
$Form1.AutoScroll = $True
$Form1.KeyPreview = $True
############################################## Buttons ##############################################
$OKButton1 = New-Object System.Windows.Forms.Button
$OKButton1.Location = New-Object System.Drawing.Size(20,$OKLoc)
$OKButton1.Size = New-Object System.Drawing.Size(75,23)
$OKButton1.Text = "OK"
$OKButton1.Add_Click({$x=$objTextBox.Text;$Form1.Close();{split};Write-Host "$arte1"})
$Form1.Controls.Add($OKButton1)
$CancelButton1 = New-Object System.Windows.Forms.Button
$CancelButton1.Location = New-Object System.Drawing.Size(150,$OKLoc)
$CancelButton1.Size = New-Object System.Drawing.Size(75,23)
$CancelButton1.Text = "Cancel"
$CancelButton1.Add_Click({$Form1.Close()})
$Form1.Controls.Add($CancelButton1)
############################################## Buttons ##############################################
if ($artefacts -eq 1) {
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(15,20)
$Label.Size = New-Object System.Drawing.Size(50,20)
$Label.Text = "File 1"
$Form1.Controls.Add($Label)
$arte = New-Object System.Windows.Forms.TextBox
$arte.Location = New-Object System.Drawing.Size(80,15)
$arte.Size = New-Object System.Drawing.Size(280,20)
$Form1.Controls.Add($arte)
}
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(270,$OKLoc)
$Button.Size = New-Object System.Drawing.Size(75,23)
$Button.Text = "Browse"
$Button.Add_Click({Button})
#Write-Host $test
$Form1.Controls.Add($Button)
#####################################################Function Button#####################################################
function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [string]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect) {
Add-Type -AssemblyName System.Windows.Forms
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Title = $WindowTitle
if (![string]::IsNullOrWhiteSpace($InitialDirectory)) {
$openFileDialog.InitialDirectory = $InitialDirectory
}
$openFileDialog.Filter = $Filter
if ($AllowMultiSelect) {
$openFileDialog.MultiSelect = $true
}
$openFileDialog.ShowHelp = $true
# Without this line the ShowDialog() function may hang depending on system configuration and running from console vs. ISE.
$openFileDialog.ShowDialog() > $null
if ($AllowMultiSelect) {
return $openFileDialog.Filenames
} else {
return $openFileDialog.Filename
}
}
function Button {
for ($a=0;$a -lt $artefacts;$a++) {
$b = $a+1
$arte[$a].Text = Read-OpenFileDialog -WindowTitle "Select File $b" -InitialDirectory "C:\" -Filter "ALL files (*.*)|*.*"
}
Write-Host "$arte"
$arte1=$arte
}
function Split {
$arte1.split('\')[1]#.split(',')[0]
Write-Host "$arte1"
}
#####################################################Function Button
if ($artefacts -gt 1) {
$b = 20
$c = 15
for ($a=0;$a -lt $artefacts;$a++) {
$d = $a + 1
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(15,$b)
$Label.Size = New-Object System.Drawing.Size(50,20)
$Label.Text = "File $d"
$Form1.Controls.Add($Label)
[Array]$arte += New-Object System.Windows.Forms.TextBox
$arte[$a].Location = New-Object System.Drawing.Size(80,$c)
$arte[$a].Size = New-Object System.Drawing.Size(280,20)
$Form1.Controls.Add($arte[$a])
$b = $b+30
$c = $c+30
}
}
$Form1.Add_Shown({$Form1.Activate()})
[void] $Form1.ShowDialog()
}
$objForm.Topmost = $false
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
如果只需要文件名,可以使用Split-Path
:
Split-Path "C:\Users\Gourav Bid\Desktop\pwd.txt" -Leaf
产出
pwd.txt
在你的例子中,你可以替换
Write-Host "$arte"
和
$arte | % { Write-Host (Split-Path $_.Text -Leaf) }
它将迭代 $arte
,并从 .Text
属性 中提取文件名。
你得到的输出是整个 TextBox
对象,你需要指定你想要哪个 属性,在本例中是 .Text
.