powershell- savefiledialog暂时创建txt文件
我想在PowerShell中创建一个GUI,在该GUI中,用户选择一个保存空TXT文件的路径。为此,我使用SaveFileDialog。当我运行下面的代码时,文件将暂时创建。它不是永久保存的。我需要改变什么?
Function fileSave
{
$OpenFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$OpenFileDialog.CheckPathExists = $true
$OpenFileDialog.CreatePrompt = true;
$OpenFileDialog.OverwritePrompt = true;
$OpenFileDialog.initialDirectory = $initialDirectory
$Directory = $OpenFileDialog.FileName = 'NewFile'
$OpenFileDialog.Title = 'Choose directory to save the output file'
$OpenFileDialog.filter = "Text documents (.txt)|*.txt"
# Show save file dialog box
if($OpenFileDialog.ShowDialog() -eq 'Ok'){
$NewFile = New-Item -Path "$Directory" -ItemType File -Force
}
I want to create a GUI in Powershell where the user selects a path to save an empty txt file. For this I use SaveFileDialog. When I run the code below, the file is created temporarily. It is not saved permanently. What do I need to change?
Function fileSave
{
$OpenFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$OpenFileDialog.CheckPathExists = $true
$OpenFileDialog.CreatePrompt = true;
$OpenFileDialog.OverwritePrompt = true;
$OpenFileDialog.initialDirectory = $initialDirectory
$Directory = $OpenFileDialog.FileName = 'NewFile'
$OpenFileDialog.Title = 'Choose directory to save the output file'
$OpenFileDialog.filter = "Text documents (.txt)|*.txt"
# Show save file dialog box
if($OpenFileDialog.ShowDialog() -eq 'Ok'){
$NewFile = New-Item -Path "$Directory" -ItemType File -Force
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想引用
fileName
属性在您的内部,如果
条件,它将是文件的绝对路径创建。除此之外,您还使用true
而不是$ true
。You want to reference the
FileName
property inside yourif
condition, it will be the absolute path of the file to be created. Aside from that, you're usingtrue
instead of$true
.