【问题标题】:Open Explorer and redirect to specific path after SaveFileDialog在 SaveFileDialog 之后打开资源管理器并重定向到特定路径
【发布时间】:2021-06-23 10:47:32
【问题描述】:

我创建了一个简单的函数来从 URL 下载文件,然后使用 SaveFileDialog 将其保存到某个文件夹,然后程序将打开 Windows 资源管理器到保存文件的路径。 但我不知道如何获取 SaveFileDialog 的最后一个路径

这是我的代码:

Dim path = "myURL"

Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.FileName = dgvAttachmentName & dgvFileExtensi
saveFileDialog1.RestoreDirectory = True

If saveFileDialog1.ShowDialog() = DialogResult.OK Then
    Using client As New WebClient()

        client.DownloadFile(path, saveFileDialog1.FileName)
        Process.Start("explorer.exe", "/root," & saveFileDialog1.FileName)

    End Using
End If

如果我使用SaveFileDialog1.FileName,我会得到文件的完整路径但带有文件名,但我也不能使用replace 从路径中删除文件名,因为用户每次都可以更改文件名他们想保存一个文件。

如何仅从 SaveFileDialog 获取路径,然后打开资源管理器到该路径?

【问题讨论】:

  • 任何时候你想对文件名和路径执行操作,我都建议你看看System.IO.Path中是否有什么可以为你做的。

标签: vb.net


【解决方案1】:

试试这个:

 Dim fi As New System.IO.FileInfo(saveFileDialog1.FileName)
 Dim Path = fi.DirectoryName

大家一起:

Dim path = "myURL"

Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.FileName = dgvAttachmentName & dgvFileExtensi
saveFileDialog1.RestoreDirectory = True

If saveFileDialog1.ShowDialog() = DialogResult.OK Then
    Using client As New WebClient()

        client.DownloadFile(path, saveFileDialog1.FileName)
        
        Dim fi As New System.IO.FileInfo(saveFileDialog1.FileName)
        Dim Path = fi.DirectoryName

        Process.Start("explorer.exe", "/root," & Path )

    End Using
End If

【讨论】:

  • 另一种方法是使用Path.GetDirectoryName(saveFileDialog1.FileName),这样可以避免创建 FileInfo 对象。
猜你喜欢
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
相关资源
最近更新 更多