Skip to content

Instantly share code, notes, and snippets.

@VenenJean
Last active September 1, 2023 17:34
Show Gist options
  • Save VenenJean/4a213b5a506b859439372b9051ea0e54 to your computer and use it in GitHub Desktop.
Save VenenJean/4a213b5a506b859439372b9051ea0e54 to your computer and use it in GitHub Desktop.

Using the 'File' (System.IO) class for reading and writing data

  • File.ReadAllText(filePath); -> Reads / Returns the data of a file as STRING

  • File.ReadAllLines(filePath); -> Reads /Returns the contents of a file and stores each line at a new index in a STRING[]

  • File.ReadAllBytes(filePath); -> Reads / Returns the contents of a file as binary data and stores the data in a BYTE[]

  • File.WriteAllText(filePath, data); -> Overwrites the content with the content of a string variable

  • File.WriteAllLines(filePath, data); -> Write content of a string array - each entry = new line

  • File.AppendAllText(filePath, data); -> Appends STRING

  • File.AppendAllLines(filePath, data); -> Append content of a string array


Using the 'File' and 'FileInfo' class to manipulate files

  • File.Copy(sourceFilePath, destinationFilePath, overWrite); -> Copying existing file to a new location

NOTE: The "overwrite parameter" passed to the "Copy method" call indicates that the copy process should overwrite an existing file if it exists at the destination path.
If you pass false to the "Copy method" call, and the file already exists then the "Common Language Runtime" (CLR) will throw a "System.IO.IOException".

  • File.Delete(sourceFilePath); -> Deletes existing file
  • File.Exists(sourceFilePath); -> Returns BOOLEAN
  • File.GetCreationTime(sourceFilePath); -> Returns creation time as DATETIME

! Instantion - REQUIRED !

  • FileInfo flInfo = new FileInfo(sourceFilePath); <- !This is the file connection!
  • flInfo.CopyTo(destinationFilePath, overwrite);

NOTE: The "overwrite parameter" in the "CopyTo method" indicates that the copy process should overwrite an existing file if it exists at the specified destination file path. If you pass false to the "CopyTo method", and the file already exists then the "Common Language Runtime" (CLR) will throw a "System.IO.IOException".

  • flInfo.Delete(); -> Deletes content of the existing file
  • flInfo.DirectoryName; -> Returns the directory path as STRING
  • flInfo.Exists; -> Return BOOLEAN
  • flInfo.Extension; -> Returns the file extension as STRING
  • flInfo.Length; -> Returns the length in bytes (LONG)

Using the 'DirectoryInfo' and 'Directory' classes to manipulate directories

  • Directory.CreateDirectory(sourceDirPath); -> Creates a new directory
  • Directory.Delete(sourceDirPath, deleteRecursivly); -> Deletes a direcotry at a specific path

NOTE: The "deleteRecursively parameter" passed into the "Delete method" specifies whether the delete process should delete any content that may exist in the directory. If you pass false into the "Delete method", and the directory is not empty then the "Common Language Runtime" CLR will throw a "System.IO.IOException".

  • Directory.Exists(sourceDirPath); -> Returns a BOOLEAN
  • Directory.GetDirectories(sourceDirPath); -> Returns a STRING[] of subdirectories within a specific directory
  • Directory.GetFiles(sourceDirPath); -> Returns a STRING[] of files within a specific directory

! Instantion - REQUIRED !

DirectoryInfo directory = new DirectoryInfo(sourceDirPath); <- !This is the directory connection!

  • directory.Create(); -> Creates new directory
  • directory.Delete(deleteRecursively); -> Deletes directory

NOTE: The "recursivelyDeleteSubContent parameter" passed to the "Delete method" call indicates whether the delete process should delete any content that may exist in the directory. If you pass false to the "Delete method" call, and the directory is not empty then the "Common Language Runtime" CLR will throw a "System.IO.IOException".

  • directory.Exists; -> Returns a BOOLEAN
  • directory.FullName; -> Returns the full path as STRING

DirectoryInfo[] subDirectories = directory.GetDirectories(); The "GetDirectories" method gets a list of all subdirectories within a specific directory on the file system. In contrast to the static File.GetDirectories method, this instance method returns an array of type DirectoryInfo, that enables you to use each of the instance properties for each subdirectory.

FileInfo[] subFiles = directory.GetFiles(); The "GetFiles" method gets a list of all the files within a specific directory on the file system. In contrast to the static File.GetFiles method, this instance method returns an array of type FileInfo, that enables you to use each of the instance properties for each file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment