3. Working with Files Flashcards

1
Q

How do you write text to a new file in C# and what happens if the specified file already exists?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you access information about a file in C#?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you return the name of a file with the extension using a FileInfo object?

A

By using its Name method.

eg. fileInfo.Name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you return the extension of a file using a FileInfo object?

A

By using its Extension method.

eg. fileInfo.Extension;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you return the size of a file in bytes using a FileInfo object?

A

By using its Length property.

eg. fileInfo.Length;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you get a file name without its extension?

A

By passing the full path including the file name to the static Path.GetFileNameWithoutExtension method.

eg. Path.GetFileNameWithoutExtension(“file.txt”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you return the full path to a file including its name using a FileInfo object?

A

By using its FullName property.

eg. fileInfo.FullName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you determine whether a file already exists?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you write a string array to a text file where each item is written into its own line?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you read all of the lines of a file while storing each line as an element inside a string array?

A

By using the static File.ReadAllLines method and passing it the path to the file.

eg. string[] lines = File.ReadAllLines(“dir/text.xt”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you remove slashes from either side of a given path string?

A

By using the TrimEnd string extension method and passing it Path.DirectorySeperatorChar static property.

eg dir.TrimEnd(Path.DirectorySeperatorChar);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you duplicate a file in C#?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you move a file in C#?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you concatenate a string array items into a single string separated by line breaks?

A

By using the static string.Join method and passing it Environment.NewLine and the string array respectively.

eg. string.Join(Environment.NewLine, strArr);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you instantiate a new bitmap image in C#?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you paint the entire canvas of a bitmap to a certain colour in C#?

A

First, you create a Graphics object by passing the bitmap object to its constructor. Then, you call the Clear method of the Graphics object by passing it the colour you’d like to paint the whole canvas with.

eg.

var g = Graphics.FromImage(bitmap);
g.Clear(Color.Magenta);
~~~
~~~

17
Q

How do you save a bitmap image from a Bitmap object?

A

By calling the Save method of the Bitmap object and passing it the file path, including the file name and the extension, and the format.

eg. bitmap.Save(imageFileName, ImageFormat.Jpeg);