Visual Studio 2017中的OpenFileDialog组件

时间:2018-06-29 19:55:16

标签: visual-studio-2017 openfiledialog

我正在Visual Studio 2017中处理VB项目。这是一个空白应用程序(通用Windows)项目。尝试使用这种类型的应用程序时,它似乎没有Windows窗体应用程序(.NET Framework)所具有的OpenFileDialog。有没有办法做以下两件事之一:

  1. 创建与空白应用程序(通用Windows)具有相同外观的Windows Forms应用程序

  2. 将OpenFileDialog选项添加到空白应用程序(通用Windows)

2 个答案:

答案 0 :(得分:0)

我最终要做的是获得相同的外观,只是玩转表单和按钮的某些属性。这给了我我所追求的外观。不完全是,但是我接受。

对于OpenFileDialog,我最终使用以下命令:

    Dim myStream As IO.Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()

    ' Open file dialog parameters
    openFileDialog1.InitialDirectory = "c:\"                    ' Default open location
    openFileDialog1.Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*"   
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Insert code to read the stream here.
                Textbox1.Text = openFileDialog1.FileName

                ' Even though we're reading the entire path to the file, the file is going to be ignored and only the path will be saved.
                ' Mostly due to me lacking the ability to figure out how to open just the directory instead of a file. Resolution threadbelow.
                ' http://www.vbforums.com/showthread.php?570294-RESOLVED-textbox-openfiledialog-folder

                ' Setting the public variable so it can be used later
                Dim exepath As String
                exepath = IO.Path.GetDirectoryName(Me.txtExeLocation.Text)

            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End Try
    End If

答案 1 :(得分:0)

以下代码是位于https://docs.microsoft.com/en-us/uwp/api/Windows.Storage.Pickers.FileOpenPicker的MS示例的VB版本。

Imports Windows.Storage

Imports Windows.Storage.Pickers

Public NotInheritable Class MainPage
    Inherits Page

    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim openPicker As New FileOpenPicker()
        openPicker.ViewMode = PickerViewMode.Thumbnail
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
        openPicker.FileTypeFilter.Add(".jpg")
        openPicker.FileTypeFilter.Add(".jpeg")
        openPicker.FileTypeFilter.Add(".png")

        Dim file As StorageFile = Await openPicker.PickSingleFileAsync()

        If (file IsNot Nothing) Then
            Debug.WriteLine($"Picked File: {file.Name}")
        Else
            Debug.WriteLine("Operation Cancelled.")
        End If

    End Sub
End Class