3. Working with Files Flashcards
How do you write text to a new file in C# and what happens if the specified file already exists?
By using the File.WriteAllText
static method and passing it the path to the file including the file name and extension as a single string and the text to be written as arguments respectively. If the file exists already, it is overwritten.
eg. File.WriteAllText(“dir/text.txt”, “Hello world!”);
How can you access information about a file in C#?
By initialising a new FileInfo
instance and passing its constructor the path to the file. You can then use the FileInfo
object and access its properties such as Name
, Extension
, and Length
to read its name, extension, and size in bytes respectively.
eg.
~~~
var fileInfo = new FileInfo(“dir/text.txt”);
fileInfo.Name; fileInfo.Extension; fileInfo.Length;
~~~
How do you return the name of a file with the extension using a FileInfo object?
By using its Name
method.
eg. fileInfo.Name;
How do you return the extension of a file using a FileInfo object?
By using its Extension
method.
eg. fileInfo.Extension;
How do you return the size of a file in bytes using a FileInfo object?
By using its Length
property.
eg. fileInfo.Length;
How do you get a file name without its extension?
By passing the full path including the file name to the static Path.GetFileNameWithoutExtension
method.
eg. Path.GetFileNameWithoutExtension(“file.txt”);
How do you return the full path to a file including its name using a FileInfo object?
By using its FullName
property.
eg. fileInfo.FullName;
How do you determine whether a file already exists?
By using the static File.Exists
method and passing it the path to the file including the file name and extension.
eg. File.Exists(“dir/text.txt”);
How do you write a string array to a text file where each item is written into its own line?
By using the static File.WriteAllLines
method and passing it the path to the file including the file name and the extension, and the string array respectively.
eg. File.WriteAllLines(“dir/text.txt”, new string[] {…});
How do you read all of the lines of a file while storing each line as an element inside a string array?
By using the static File.ReadAllLines
method and passing it the path to the file.
eg. string[] lines = File.ReadAllLines(“dir/text.xt”);
How do you remove slashes from either side of a given path string?
By using the TrimEnd
string extension method and passing it Path.DirectorySeperatorChar
static property.
eg dir.TrimEnd(Path.DirectorySeperatorChar);
How do you duplicate a file in C#?
By using the static File.Copy
method and passing it the file’s path including the name and extension, and a path to a new file respectively.
eg. File.Copy(“dir/foo.txt”, “temp/bar.txt”);
How do you move a file in C#?
By using the static File.Move
method and passing it the file’s path including the name and extension, and the path for it to be moved to, respectively.
eg. File.Move(“dir/foo.txt”, “temp/foo.txt”);
How do you concatenate a string array items into a single string separated by line breaks?
By using the static string.Join
method and passing it Environment.NewLine
and the string array respectively.
eg. string.Join(Environment.NewLine, strArr);
How do you instantiate a new bitmap image in C#?
By instantiating the System.Drawing.Bitmap
class that comes with the System.Drawing.Common
nuget package, and passing its constructor the width, the height, and the format of the bitmap.
eg. new Bitmap(128, 128, PixelFormat.Format24bppRgb);