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.
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
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
StreamReader
and StreamWriter
from System.IO
namespace are used respectively to read and write from or to text files.
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
}
}
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);
}
FileStream
with BinaryReader
and BinaryWriter
from System.IO
namespace are used to respectively read and write from or to binary text files.
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;
}
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);
}
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;
}
}
XmlDocument()
from System.Xml
namespace is used to respectively load XML files.
using System;
using System.Xml;
string path = "path/to/read/xmlFile.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path);
Once a XmlDocument
is created you can search nodes inside it:
using System;
using System.Xml;
XmlNode node = doc.DocumentElement.SelectSingleNode("/link/to/node"); // doc exists and is a XmlDocument
foreach(XmlNode node in doc.DocumentElement.ChildNodes){
// Do something on child nodes
}
string text = node.InnerText;
string attrText = node.Attributes["attributeName"]?.InnerText;
See: