C# GetFiles

weixin_34115824發表於2018-03-09
                var path = AppDomain.CurrentDomain.BaseDirectory + "Images\\Rooms\\";
                //    string[] patterns = new[] { "*.jpg", "*.jpeg", "*.jpe", "*.jif", "*.jfif", "*.jfi", "*.webp", "*.gif", "*.png", "*.apng", "*.bmp", "*.dib", "*.tiff", "*.tif", "*.svg", "*.svgz", "*.ico", "*.xbm" };
                string[] patterns = new[] { "*.jpg", "*.png" };
                string[] files = patterns.SelectMany(pattern => Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly)).Distinct().ToArray();

                // For.NET 4.0 +
                var files1 = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg"));

                // For 2.0,
                var files2 = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg"));

                //不支援多種型別同時獲取
                FileInfo[] fileInfos = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Images\\Rooms\\").GetFiles("*.jpg");