首页 > 技术文章 > 公司代码阅读笔记 记于 2013-09-23

changweihua 2013-09-23 10:38 原文

标签: `代码` `C#` `WPF`

**遍历文件夹下任意类型的文件**

```cs
    Loaded += delegate {
    
        var arr = "*.png|*.jpg|*.jpeg|*.gif".Split('|').SelectMany(filter => new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)).GetFiles(filter, SearchOption.AllDirectories)).ToArray();

    };
```

---

```cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CFileWall
{
    /// 
    /// LoadingUserControl.xaml 的交互逻辑
    /// 
    public partial class LoadingUserControl : UserControl
    {
        public event EventHandler LoadingCompleted;

        public LoadingUserControl()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(LoadingUserControl_Loaded);
        }

        void LoadingUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //启动新的线程加载文件
            new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(delegate
            {
                int index = 1;
                if (App.Files == null) return;
                foreach (var file in App.Files)
                {
                    this.Dispatcher.Invoke((Action)delegate
                    {
                        Console.WriteLine(index);

                        textBlock_LoadingTips.Text = index.ToString() + @"/" + App.Files.Length.ToString();
                        loadingProgress.Value = (double)index / App.Files.Length * 100;
                    }, null);

                    System.Threading.Thread.Sleep(100);

                    index++;
                }
                if (LoadingCompleted != null)
                {
                    this.Dispatcher.BeginInvoke((Action)delegate
                    {
                        DispatcherTimer dt = new DispatcherTimer();
                        dt.Interval = TimeSpan.FromSeconds(1);
                        dt.Tick += delegate
                        {
                            dt.Stop();
                            LoadingCompleted(this, null);
                        };
                        dt.Start();
                    }, null);
                }
            })).Start();
        }
    }
}


```

有问题或建议请反馈到:

微博 :[@Changweihua](http://weibo.com/changweihua)

邮箱 :changweihua@outlook.com  

推荐阅读