DirectoryInfo di = new DirectoryInfo(@"\\192.168.0.1\old"); //所有檔案列表包含子資料夾 var queryfileall = di.GetFiles("*", SearchOption.AllDirectories); //指定單一副檔名 var queryfileone = di.GetFiles("*.jpg", SearchOption.AllDirectories); //指定多重副檔名 string[] extensions = new[] { ".jpg", ".mp3"}; var queryfilemultiple = di.GetFiles("*", SearchOption.AllDirectories).Where(x => extensions.Contains(x.Extension.ToLower())).ToArray(); //建立資料夾 string newpath = @"\\192.168.0.1\new"; if (!Directory.Exists(newpath)) { Directory.CreateDirectory(newpath); } foreach (var item in queryfile) { try { //可自行變化... //複製檔案 if (!File.Exists(newpath + item.Name)) item.MoveTo(newpath + item.Name); } catch (Exception ex) { throw new Exception(ex.Message); } } //方法2 DirectoryInfo root = new DirectoryInfo(@"\\192.168.0.1\old"); FileInfo[] files = root.GetFiles(); foreach (var item in files) {}參考來源
Get multiple files with different extension