将文件夹浏览器对话框更改为打开文件对话框 [英] Changing folderbrowserdialog to openfiledialog

查看:79
本文介绍了将文件夹浏览器对话框更改为打开文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试在wpf项目中将folderbrowserdialog更改为openfiledialog.问题是,当我将代码更改为openfiledialog时,mp3文件将不会显示在列表视图中.我尝试编写仅具有列表视图和打开openfiledialog的按钮的测试程序,它工作正常,但是当我将测试代码添加到原始代码中时,它将无法正常工作.我询问原始代码的创建者,他建议我从中获取选定文件的列表并填充列表视图",但是当我更改linq表达式时,该应用程序将无法正常运行.

这是我认为会导致问题的代码的原始部分:

Hello,
I am trying to change folderbrowserdialog to openfiledialog in a wpf project. The problem is that when I alter the code to openfiledialog the mp3 files will not show up in the listview. I have tried writing a test program with just a listview and a button which opens the openfiledialog and it works fine, but when I add the test code to the original code it does not work. I asked the creator of the original code and he suggested that I needed to, "get the list of selected files from and populate the listview" but when I change the linq expression the application does not work as well.

Here is the original part of the code which I believe is causing the problem:

Private Sub collectTrackInfo(ByVal targetFolder As String)
	  btnSelectDir.IsEnabled = False
	  tbTargetFolder.IsEnabled = False

	  Dim mp3_files = From f In Directory.GetFiles(targetFolder)
	                  Where System.IO.Path.GetExtension(f) = ".mp3"
	                  Select f

	  mp3_files = mp3_files.Reverse()
	  _fileCount = mp3_files.Count()
	  _results.Clear()
	  If _fileCount = 0 Then
		status.Text = "No MP3 files in this folder."
		btnSelectDir.IsEnabled = True
		tbTargetFolder.IsEnabled = True
	  Else
		_trackDurations = New TrackDurations(mediaElement, mp3_files, AddressOf onTrackInfo
	  End If



这是我为了修复它而尝试做的事情:



And here is what I tried to do in order to fix it:

Private Sub collectTrackInfo(ByVal targetFolder As String)
	 btnSelectDir.IsEnabled = False
	 tbTargetFolder.IsEnabled = False

      Dim mp3_files = files.SelectedValue

   _fileCount = mp3_files.Count()
	 _results.Clear()
	 If _fileCount = 0 Then
		status.Text = "No MP3 files in this folder."
		btnSelectDir.IsEnabled = True
		tbTargetFolder.IsEnabled = True
	 Else
		_trackDurations = New TrackDurations(mediaElement, mp3_files, AddressOf onTrackInfo)
	 End If



预先感谢您提供的任何帮助,代码或建议,或者如果您需要任何其他信息以更好地理解,请告诉我.



Thanks in advance for any help, code, or suggestions which you may provide, or if you need any additional information in order to get a better understanding please let me know.

推荐答案

要获取在OpenFileDialog中选择的文件,请使用以下代码:
To get the files selected in an OpenFileDialog, use this code:
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "MP3 Files (*.mp3)|*.mp3";
dialog.FilterIndex = 1;

dialog.CheckFileExists = true;
dialog.Multiselect = true;
string[] selectedFiles = null;

if (dialog.ShowDialog() == DialogResult.OK)
{
	selectedFiles = dialog.FileNames;
}


然后,您可以在代码中使用字符串数组selectedFiles.


You can then use the string array selectedFiles in your code.


您实际上并未显示相关代码.您不能互换使用这两个控件.当您要选择文件夹时,请使用FolderBrowserDialog;当您要选择文件时,请使用OpenFileDialog.
You have actually not shown the relevant code. You cannot interchangeably use these two controls. Use FolderBrowserDialog when you want to select a folder and use OpenFileDialog when you want to select files.


我知道我没有显示openfiledialog代码,因为我觉得它已经存在我已经理解了该领域中应该包含的内容,而是选择询问有关我认为会产生问题的代码部分的问题.我与代码的创建者进行了交谈,他说很有可能将folderbrowserdialog更改为openfiledialog,我也相信.我也了解了folderbrowserdialog和openfiledialog之间的目的和差异.

为了便于讨论,下面是包含文件夹browserdialog的原始代码:

I understand that I did not show the openfiledialog code because I felt it is already understood what should be in that field already, instead I chose to ask my question about the part of the code which I thought was producing the problem. I talked to the creator of the code and he said that it was very possible to change the folderbrowserdialog to openfiledialog which I believe as well. I understand the purpose and differences between folderbrowserdialog and openfiledialog also.

For the sake of argument here is the original code with the folderbrowserdialog included:

Imports System.Text
Imports System.IO
Imports System.Globalization
Imports System.Collections.ObjectModel

Namespace MP3DurationCalculator
  
  Partial Public Class MainWindow
	  Inherits Window
	Public Sub New()
	  InitializeComponent()
	  _folderBrowserDialog = New FolderBrowserDialog()
	  _folderBrowserDialog.Description = "Select the directory that containd the MP3 files."
	  ' Do not allow the user to create new files via the FolderBrowserDialog.
	  _folderBrowserDialog.ShowNewFolderButton = False
	  _folderBrowserDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory
	  Dim dt = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
	  Dim start = System.IO.Path.Combine(dt, "Downloads")
	  _folderBrowserDialog.SelectedPath = start
	  _results = New ObservableCollection(Of TrackInfo)()
            files.ItemsSource = _results
	End Sub

	Private Sub collectTrackInfo(ByVal targetFolder As String)
	  btnSelectDir.IsEnabled = False
	  tbTargetFolder.IsEnabled = False

	  Dim mp3_files = From f In Directory.GetFiles(targetFolder)
	                  Where System.IO.Path.GetExtension(f) = ".mp3"
	                  Select f

	  mp3_files = mp3_files.Reverse()
	  _fileCount = mp3_files.Count()
	  _results.Clear()
	  If _fileCount = 0 Then
		status.Text = "No MP3 files in this folder."
		btnSelectDir.IsEnabled = True
		tbTargetFolder.IsEnabled = True
	  Else
		_trackDurations = New TrackDurations(mediaElement, mp3_files, AddressOf onTrackInfo)
	  End If

	End Sub

	Private Sub btnSelectDir_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	  Dim r As DialogResult = _folderBrowserDialog.ShowDialog()
            If r = Forms.DialogResult.OK Then
                tbTargetFolder.Text = Me._folderBrowserDialog.SelectedPath
            End If

	  
	End Sub

	Private Sub onTrackInfo(ByVal ti As TrackInfo)
	  If ti Is Nothing Then
		_maxLength = 0
		_trackDurations.Dispose()
		status.Text = "Ready."
		btnSelectDir.IsEnabled = True
		tbTargetFolder.IsEnabled = True
	  Else
		_results.Add(ti)
		' Make sure the new filename fits in the column
                Dim ft = New FormattedText(ti.Name, CultureInfo.GetCultureInfo("en-us"), Windows.FlowDirection.LeftToRight, New Typeface(files.FontFamily, files.FontStyle, files.FontWeight, files.FontStretch), files.FontSize, System.Windows.Media.Brushes.Black)

		If ft.Width > _maxLength Then
		  _maxLength = ft.Width
		  Dim gv = CType(files.View, GridView)
		  Dim gvc = gv.Columns(0)
		  Dim curWidth = gvc.Width

		  ' Reset to a specific width before auto-sizing
		  gvc.Width = _maxLength
		  ' This causes auto-sizing
		  gvc.Width = Double.NaN

		End If

		' Update the status line
		Dim st = String.Format("Collecting track info {0}/{1} ...", _results.Count, _fileCount)
		status.Text = st
	  End If
	End Sub

	Private Sub files_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
	  Dim tp = New TimeSpan()
	  For Each f In files.SelectedItems
		tp = tp + (CType(f, TrackInfo)).Duration
	  Next f

	  Dim d = New Date(tp.Ticks)
	  Dim format As String = "mm:ss"
	  If tp.Hours > 0 Then
		format = "hh:mm:ss"
	  End If

	  total.Text = d.ToString(format)
	End Sub

	Private Sub SelectAll_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	  files.SelectAll()
	End Sub

	Private Sub UnselectAll_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	  files.UnselectAll()
	End Sub

	Private Sub tbTargetFolder_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
	  If Directory.Exists(tbTargetFolder.Text) Then
		collectTrackInfo(tbTargetFolder.Text)
	  End If
	End Sub ' the length of the longest track name

	Private _folderBrowserDialog As FolderBrowserDialog
	Private _trackDurations As TrackDurations
	Private _results As ObservableCollection(Of TrackInfo)
	Private _fileCount As Integer = 0
	Private _maxLength As Double = 0

  End Class

  <ValueConversion(GetType(TimeSpan), GetType(String))>
  Public Class DurationConverter
	  Implements IValueConverter
	Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
	  Dim duration = CType(value, TimeSpan)
	  Dim d = New Date(duration.Ticks)
	  Dim format As String = "mm:ss"
	  If duration.Hours > 0 Then
		format = "hh:mm:ss"
	  End If

	  Return d.ToString(format)
	End Function

	Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
		Throw New NotImplementedException()
	End Function
  End Class

End Namespace



我需要的是collectTrackInfo类,更具体地说是我可以使用什么linq表达式或替代代码从openfiledialog中仅获取选定的文件,而不是将targetFolder中的所有文件作为提供的代码中的linq表达式来获取.提前谢谢...

(我已经知道我没有更改folderbrowser对话框,这是修改之前的原始代码.)



All I need help with is the collectTrackInfo class, more specifically what linq expression or alternative code can I use to get only the selected files from openfiledialog instead of getting all files in the targetFolder as the linq expression in the provided code calls for. Thanks in advance...

(I already know that I did not change folderbrowser dialog, this is the original code before any modifications.)


这篇关于将文件夹浏览器对话框更改为打开文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆