Revision

Back to C#


Scan folder

Directory or DirectoryInfo from System.IO namespace can be used to list files and directories from a given path. For both of the methods you can apply filtering rules.

Using Directory

  using System;
  using System.IO;

  string path = "path/to/folder";

  string[] subdirs = Directory.GetDirectories(path, "*");
  foreach(string subdir in subdirs)
    Console.WriteLine(subdir); // print full path to sub directory

  string[] files = Directory.GetFiles(path, "*");
  foreach(string file in files)
    Console.WriteLine(file); // print full path to file


Using DirectoryInfo

  using System;
  using System.IO;

  string path = "path/to/folder";
  DirectoryInfo directoryInfo = new DirectoryInfo(path);

  DirectoryInfo[] subDirs= directoryInfo.GetDirectories();
  foreach(DirectoryInfo subDir in subDirs)
    Console.WriteLine(subDir.FullName); // print full path to sub directory

    FileInfo[] files = directoryInfo.GetFiles();
  foreach(FileInfo file in files)
    Console.WriteLine(file.FullName); // print full path to file


Text Files

StreamReader and StreamWriter from System.IO namespace are used respectively to read and write from or to text files.


Read

  using System;
  using System.IO;

  string path = "path/to/read/file.txt";
  string line;
  using (StreamReader stream = new StreamReader(path)) {
    while ((line = stream.ReadLine()) != null) {
      // Do something on line
    }
  }


Write

  using System;
  using System.IO;

  string path = "path/to/write/file.txt";
  string[] lines = new string[] { "First line.", "Second line." };
  using (StreamWriter outputFile = new StreamWriter(path)) {
    foreach(string line in lines)
      outputFile.WriteLine(line);
  }


Binary Text Files

FileStream with BinaryReader and BinaryWriter from System.IO namespace are used to respectively read and write from or to binary text files.


Read

  using System;
  using System.IO;

  string path = "path/to/read/binaryfile";
  using (FileStream inputStream = new FileStream(path, FileMode.Open))
  using (BinaryReader reader = new BinaryReader(inputStream)) {
    long length = inputStream.Length;
    const int bufferSize = 1000000;
    while (length > 0) {
      byte[] buffer = reader.ReadBytes(bufferSize);
      // Do something on buffer
      length -= bufferSize;
  }


Write

  using System;
  using System.IO;

  string path = "path/to/write/binaryfile";
  byte[][] binaryTexts = new bytes[][]; // Empty here but normally filles

  using (FileStream outputStream = new FileStream(path, FileMode.Create))
  using (BinaryWriter writer = new BinaryWriter(outputStream)) {
    foreach(byte[] binaryText in binaryTexts)
      writer.Write(binaryText);
  }


Read from one binary file and Write in another one

  using System;
  using System.IO;

  string pathFrom = "path/to/read/binaryfile";
  string pathTo = "path/to/write/binaryfile";

  using (FileStream inputStream = new FileStream(pathFrom, FileMode.Open))
  using (FileStream outputStream = new FileStream(pathTo, FileMode.Create))
  using (BinaryReader reader = new BinaryReader(inputStream))
  using (BinaryWriter writer = new BinaryWriter(outputStream))
  {
      long length = inputStream.Length;

      const int bufferSize = 1000000;
      while (length > 0) {
          byte[] buffer = reader.ReadBytes(bufferSize);
          writer.Write(buffer);
          length -= bufferSize;
      }
  }


XML

XmlDocument() from System.Xml namespace is used to respectively load XML files.


Read

  using System;
  using System.Xml;

  string path = "path/to/read/xmlFile.xml";

  XmlDocument doc = new XmlDocument();
  doc.Load(path);


Finding nodes

Once a XmlDocument is created you can search nodes inside it:


By absolute path

  using System;
  using System.Xml;

  XmlNode node = doc.DocumentElement.SelectSingleNode("/link/to/node"); // doc exists and is a XmlDocument


Children of a node

  foreach(XmlNode node in doc.DocumentElement.ChildNodes){
    // Do something on child nodes
  }


Extract Information

Getting text of the node

  string text = node.InnerText;


Getting text of an attribute of the node

  string attrText = node.Attributes["attributeName"]?.InnerText;


Ressources

See: