【发布时间】:2020-06-23 04:32:43
【问题描述】:
我需要在 PowerShell GUI 上创建一个按钮,以便在按下时保存 DataGridView 的内容。这需要打开一个文件对话框来选择目的地并给它一个文件名。
查看我已经尝试过的代码
$btnSave_Click={
$saveFile = New-Object System.Windows.Forms.SaveFileDialog
$saveFile.Filter = "Comma Delimited (*.csv) | *.csv "
$saveFile.FileName
if ($saveFile.ShowDialog() -eq 'OK')
{
$results.SelectAll()
$results.ClipboardCopyMode = 'EnableAlwaysIncludeHeaderText'
$results.GetClipboardContent().GetData('Text') | Out-File
$saveFile.FileName
}
}
收到的错误是
ERROR: You cannot call a method on a null-valued expression.
ERROR: char:4
ERROR: + $results.SelectAll()
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [],
RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:
ERROR: The property 'ClipboardCopyMode' cannot be found on this object.
Verify that the property exists and can be set.
ERROR: char:4
ERROR: + $results.ClipboardCopyMode
= 'EnableAlwaysIncludeHea ...
ERROR: +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [],
RuntimeException
ERROR: + FullyQualifiedErrorId : PropertyNotFound
ERROR:
ERROR: You cannot call a method on a null-valued expression.
ERROR: char:4
ERROR: + $results.GetClipboardContent().GetData('Text') |
Out ...
ERROR: +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [],
RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:
【问题讨论】:
-
你没有保存
$results所以变量不存在... -
首先你需要验证'$results'变量,错误表明它为NULL。