Posted By

aubreyWlove on 09/01/18


Tagged

aspnet


Versions (?)

List Files in a Folder


 / Published in: C#
 

This is a simple "file list" program written in ASP.NET C#. It is broken down into 3 pages, Main / default page, A CLASS page named "FileList" and the "app.config" page. Create a new blank C# console application and create these 3 pages then just copy/paste the code. In the "app.config" page, you will need to change the source folder "TestFolderD" to your source folder and the list destination folder "TextList" to where you want the csv file saved to. You can also replace csv with txt if you would like it saved as a text file. Enjoy.

  1. <!-- Main / default.aspx page -->
  2. using System;
  3.  
  4.  
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. FileList.List1();
  12. }
  13. }
  14. }
  15.  
  16. <!-- Class Page (FileList) -->
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.IO;
  23. using System.Configuration;
  24.  
  25.  
  26. namespace ConsoleApplication1
  27. {
  28. class FileList
  29. {
  30. public static void List1()
  31. {
  32. string pathA = ConfigurationManager.AppSettings["TestFolderD"];
  33. string pathB = ConfigurationManager.AppSettings["TextList"];
  34.  
  35. //DirectoryInfo di = new DirectoryInfo("c:/Bank/TestFolderD");
  36. DirectoryInfo di = new DirectoryInfo(pathA);
  37. FileInfo[] fiArr = di.GetFiles("*.*", SearchOption.AllDirectories);
  38.  
  39. using (System.IO.StreamWriter file = new System.IO.StreamWriter (pathB))
  40. {
  41. foreach (FileInfo f in fiArr)
  42. {
  43. Console.WriteLine(@"Copying {0}, {1}, {2}, {3}, {4}", f.DirectoryName, f.Name, f.CreationTime, f.Length, f.LastAccessTime);
  44. file.WriteLine("{0}, {1}, {2}, {3}, {4}", f.DirectoryName, f.Name, f.CreationTime, f.Length, f.LastAccessTime);
  45. }
  46. }
  47. }
  48. }
  49. }
  50.  
  51. <!-- App.config page -->
  52. <?xml version="1.0" encoding="utf-8" ?>
  53. <configuration>
  54. <startup>
  55. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  56. </startup>
  57. <appSettings>
  58. <add key="TestFolderD" value="c:\Bank\TestFolderD"/>
  59. <add key="TextList" value="c:\Bank\List\listOfFiles4.CSV"/>
  60. </appSettings>
  61. </configuration>

Report this snippet  

You need to login to post a comment.