powershell- savefiledialog暂时创建txt文件

发布于 2025-02-09 07:56:29 字数 702 浏览 3 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2025-02-16 07:56:29

您想引用 fileName属性在您的内部,如果条件,它将是文件的绝对路径创建。除此之外,您还使用true而不是$ true

Add-Type -AssemblyName System.Windows.Forms

Function fileSave {
    $saveFileDialog = [System.Windows.Forms.SaveFileDialog]@{
        CheckPathExists  = $true
        CreatePrompt     = $true
        OverwritePrompt  = $true
        InitialDirectory = [Environment]::GetFolderPath('MyDocuments')
        FileName         = 'NewFile'
        Title            = 'Choose directory to save the output file'
        Filter           = "Text documents (.txt)|*.txt"
    }

    # Show save file dialog box
    if($saveFileDialog.ShowDialog() -eq 'Ok') {
        New-Item -Path $saveFileDialog.FileName -ItemType File -Force
    }
}

fileSave

You want to reference the FileName property inside your if condition, it will be the absolute path of the file to be created. Aside from that, you're using true instead of $true.

Add-Type -AssemblyName System.Windows.Forms

Function fileSave {
    $saveFileDialog = [System.Windows.Forms.SaveFileDialog]@{
        CheckPathExists  = $true
        CreatePrompt     = $true
        OverwritePrompt  = $true
        InitialDirectory = [Environment]::GetFolderPath('MyDocuments')
        FileName         = 'NewFile'
        Title            = 'Choose directory to save the output file'
        Filter           = "Text documents (.txt)|*.txt"
    }

    # Show save file dialog box
    if($saveFileDialog.ShowDialog() -eq 'Ok') {
        New-Item -Path $saveFileDialog.FileName -ItemType File -Force
    }
}

fileSave
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文