[英]Visual Studio PowerShell Drag & Drop Attachments From Outlook to List Box
我已經在我的 Windows 窗體上進行了拖放操作。 我可以從我的桌面或任何文件夾中刪除項目,但如果我嘗試直接從 Outlook 中拖動附件,它不會做任何事情。 我是否需要在當前代碼中添加額外的 PowerShell 命令
######################################## This is For Drag And Drop
$listBox1_DragOver = [System.Windows.Forms.DragEventHandler]{
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop))
{
$_.Effect = 'Copy'
}
Else
{
$_.Effect = 'None'
}
}
$listBox1_DragDrop = [System.Windows.Forms.DragEventHandler]{
foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop))
{
$listBox1.Items.Add($filename)
}
}
### Add events to form ###
$listBox1.Add_DragOver($listBox1_DragOver)
$listBox1.Add_DragDrop($listBox1_DragDrop)
#$form.Add_FormClosed($form_FormClosed)
#### Show form and return result ###
$dialogResult = $Form12.ShowDialog()
if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK)
{
$Form12.SuspendLayout()
[array]$items = $listbox1.Items| sort -CaseSensitive
if ($items.Count -gt 1){
$items
}
ELSE
{
[string]$items[0]
}
$Form12.Close() | out-null
}
經過大量研究,我發現了下面的代碼,總結了它的作用。
它將允許您將文件從 Outlook 中拖放,然后將其復制並粘貼到一個文件夾中,然后該文件夾為您提供路徑和文件名。 這很酷,所以如果其他人被困在這里是我的工作腳本以及我如何將它實現到我的表單中
######################################## This is For Drag And Drop
$Listbox1.AllowDrop = $true
$Listbox1.Add_DragDrop({
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
foreach ($FileName in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) {
Copy-Item -Path $FileName -Destination $textbox6.text -Force
$Listbox1.Items.Add($FileName)
}
}
else
{
$Outlook = New-Object -ComObject Outlook.Application;
$Selection = $Outlook.ActiveExplorer().Selection
foreach ($Item in $Selection) {
foreach ($Attachment in $Item.Attachments) {
Write-Verbose $Attachment.FileName
$Name = Join-Path -Path $textbox6.text-ChildPath $Attachment.FileName
$Attachment.SaveAsFile($Name)
}
}
}
})
$Listbox1.Add_DragEnter({$_.Effect = [Windows.Forms.DragDropEffects]::Copy})
$Form12.Controls.Add($Listbox1)
# Activate the form
[void] $Form12.ShowDialog()
我不得不通過添加一個Input Folder Button
& Textbox6
來顯示已選擇的文件夾的文本來稍微更改我的表單,這很重要,因為上面的腳本需要一個目錄來保存文件,請參閱下面的代碼。
###################################### Get Folder Using Folder Browser and output text into textbox
$button4_Click = {
$folderBrowserDialog3=New-Object System.Windows.Forms.FolderBrowserDialog
[void]$folderBrowserDialog3.ShowDialog()
$folderBrowserDialog3.SelectedPath
$textBox6.Text = $folderBrowserDialog3.SelectedPath
}
$button6_Click = {
$folderBrowserDialog1=New-Object System.Windows.Forms.FolderBrowserDialog
[void]$folderBrowserDialog1.ShowDialog()
$folderBrowserDialog1.SelectedPath
$textBox2.Text = $folderBrowserDialog1.SelectedPath
}
$button7_Click = {
$folderBrowserDialog2=New-Object System.Windows.Forms.FolderBrowserDialog
[void]$folderBrowserDialog2.ShowDialog()
$folderBrowserDialog2.SelectedPath
$textBox3.Text = $folderBrowserDialog2.SelectedPath
}
在我得到它之后,最讓我煩惱的是我看不到listbox
的文件,所以我添加了一個button
來做到這一點,請參閱下面的代碼
###################################### Shows Files In ListBox 1
$button5_Click = {
#$textbox8.Text = ""
$listBox1.Items.Clear()
$items = Get-ChildItem $textbox6.Text
ForEach($item in $items){
$listBox1.Items.Add($item.FullName)
}
}
如您所見,我添加了$listbox1.Items.Clear()
這將使您能夠繼續單擊顯示文件按鈕,而不會復制listbox
的路徑和文件
我的Form
的最終結果非常好,請參閱下面的圖像布局,如果有人需要幫助以使其在您自己的Form
上工作,請發表評論,我會盡力提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.