【问题标题】:How to obtain path with mapped network drive from .NET file dialog如何从 .NET 文件对话框中获取映射网络驱动器的路径
【发布时间】:2014-11-01 02:50:19
【问题描述】:

我看到很多关于如何从映射的驱动器号中获取 UNC 路径的问题,但没有看到相反的问题。

在不同的 Windows 7 Enterprise 机器上,对于具有完全相同的输入、当前目录和网络驱动器映射的 System.Windows.Forms.OpenFileDialog 实例中的 FileName,我得到了不同的答案:

  1. 一台计算机使用 UNC 路径回答

    PS A:\Liver\Department\Records\check log> .\sign-log.ps1
    VERBOSE: Writing signature to \\liver53-pc.ad.institute.edu\Documents\Liver\Department\Records\check log\00\check log.xlsx.sig
    
  2. 其他人使用映射的驱动器路径回答

    PS A:\Liver\Department\Records\check log> .\sign-log.ps1
    VERBOSE: Writing signature to A:\Liver\Department\Records\check log\00\check log.xlsx.sig
    

我需要每台计算机都使用映射的驱动器路径进行回答:接收FileName 的命令行程序不理解 UNC 路径。 以下是sign-log.ps1的内容:

Add-Type -AssemblyName System.Windows.Forms
function Sign-Log ($file = '.\check-log.xlsx') {
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Title = 'Select'
        ; InitialDirectory = (Get-Location).Path
        ; Filename = 'check log.xlsx'
        ; Filter = 'Excel Spreadsheet (*.xlsx;*.xls)|*.xlsx;*.xls|Any file (*.*)|*.*'
        ; ShowHelp = $true
    }
    if ($FileBrowser.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
        $file = $FileBrowser.FileName
        $dest = $file + '.sig'
        if (Test-Path -Path $dest) {
            Write-Verbose -Message ('Appending signature to {0}' -f $dest)
            Add-Content -Path $dest -Value (gpg '-ao' '-' '-b' $file)
        } else {
            Write-Verbose -Message ('Writing signature to {0}' -f $dest)
            gpg '-ao' $dest '-b' $file
        }
        gpg '--verify' $dest
    }
    $FileBrowser.Dispose()
}
Sign-Log

我需要做什么才能始终获得映射的驱动器路径?

【问题讨论】:

    标签: .net windows powershell openfiledialog


    【解决方案1】:

    我认为最好的办法是测试返回的 UNC 路径,如果是,暂时将其映射到驱动器号,然后将其发送到 gpg。也许是这样的:

    if (([uri]$dest).IsUnc) {
        $destPath = $dest | Split-Path -Parent
        # Find a free drive letter
        $letter = ls function:[d-z]: -n | ?{ !(test-path $_) } | Select-Object -Last 1
        New-PSDrive -Name $letter[0] -Root $destPath -Persist -PSProvider FileSystem
        $dest = $destPath | Join-Path -ChildPath $file
    }
    
    # Then after you're done calling gpg:
    if ($destPath) {
        Remove-PSDrive -Name $letter[0] -Force
    }
    

    这是未经测试的..

    获取免费驱动器号的单线来自这里:Getting a free drive letter

    New-PSDrive Reference

    Remove-PSDrive Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 2023-03-29
      • 2021-08-28
      • 1970-01-01
      相关资源
      最近更新 更多