Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="http://msdn.microsoft.com/en-us/library/dd383458.aspx" rel="nofollow"><code>Directory.EnumerateFiles</code></a> instead of <code>Directory.GetFiles</code>. <code>EnumerateFiles</code> will return the files as they're found by the system - not wait for all of them to be found.</p> <p>Do this on a background thread and use <code>Dispatcher.Invoke</code> or <code>Dispatcher.BeginInvoke</code> to the UI thread as each one is found to add it to the <code>ListBox</code>.</p> <p>Here's a quick example I threw together. Here's the XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ListBox x:Name="_FileList" /&gt; &lt;Button Grid.Row="1" Content="Go!" Click="Button_Click" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>and here's the code-behind:</p> <pre><code>using System; using System.IO; using System.Windows; namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { private Action&lt;string&gt; _AddToListBox; public MainWindow() { InitializeComponent(); _AddToListBox = AddToListBox; } private void Button_Click(object sender, RoutedEventArgs e) { Action action = Go; action.BeginInvoke(null, null); } private void Go() { foreach (var file in Directory.EnumerateFiles(@"c:\windows\system32\")) { Dispatcher.BeginInvoke(_AddToListBox, file); } } private void AddToListBox(string toAdd) { _FileList.Items.Add(toAdd); } } } </code></pre> <p>by no means do I intend for this to be a best-practices or all-encompassing example. Just showing you one way to do it. By the way, I picked the system32 directory just because it has a lot of files in it so I could test it. Still works almost instantly on my machine though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload