question from e-learning

T

Tony Johansson

Hi!

At the end I have a question from e-learning.
Because of the question is saying that there will be several writings we
need to have an object we can't use the static class
File. So we have b and d to choose from. The correct answer accorning to
e-learning is d.
Here is the motivation
If you are going to reuse an object several times, you should use the
instance method of the FileInfo class instead of the corresponding static
methods of the File class, because a security check will not always be
necessary. This will minimize any overhead and improve the performance of
the application.

You should use the following code to append data to the log file:
FileInfo fi = new FileInfo(@"c:\application.log");
StreamWriter sw = fi.AppendText();

Using the following code is incorrect because you should use FileMode.Append
only in conjunction with FileAccess.Write.
FileInfo fi = new FileInfo(@"c:\application.log");
FileStream fs = fi.Open(FileMode.Append);

Now to my question why do they say the following
Using the following code is incorrect because you should use FileMode.Append
only in conjunction with FileAccess.Write.
FileInfo fi = new FileInfo(@"c:\application.log");
FileStream fs = fi.Open(FileMode.Append);

I can compile the following two rows without any kind of error despite that
e-learning is saying is incorrect

FileInfo fi = new FileInfo(@"c:\application.log");
FileStream fs = fi.Open(FileMode.Append);

Start e-learning question
******************
You are developing a logging module for a large application by using the
..NET Framework.
You need to append logging information to a file named application.log. This
log file is opened when the application is started and is closed only when
the application is closed. However, you append text several times to the
file during a session.

You must minimize overhead to the logging process to ensure maximum
performance.

Which code segment should you use to create the log file?

a. StreamWriter sw = File.CreateText(@"c:\application.log");

b. FileInfo fi = new FileInfo(@"c:\application.log");
FileStream fs = fi.Open(FileMode.Append);

c. StreamWriter sw = File.AppendText(@"c:\application.log");

d. FileInfo fi = new FileInfo(@"c:\application.log");
StreamWriter sw = fi.AppendText();

//Tony
 
H

Harlan Messinger

Tony said:
Hi!

At the end I have a question from e-learning.
Because of the question is saying that there will be several writings we
need to have an object we can't use the static class
File.

This doesn't make any sense to me. Under the terms of the problem, you
aren't reopening the same file several times during the execution of the
application. You are opening it once. It makes no difference whether you
do this using a static method of the static File class or using an
instance of the FileInfo class that you will use once to open the file
and then will never use again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top