elearning info not working

M

mp

according to online elearning (ms)
to write to a debug log use the following:
'd. FileInfo fi = new FileInfo(@"c:\application.log");

' StreamWriter sw = fi.AppendText();

I can't seem to convert that to vb successfully

Dim fi As FileInfo = New FileInfo(m_DebugFileName)



'this didn't work

Dim sw As StreamWriter = New StreamWriter(fi.AppendText())

'so i tried this and still no joy

Dim sw As StreamWriter = New StreamWriter(fi)

Error 1 Overload resolution failed because no accessible 'New' can be called
with these arguments:
'Public Sub New(path As String)': Value of type 'System.IO.FileInfo'
cannot be converted to 'String'.
'Public Sub New(stream As System.IO.Stream)': Value of type
'System.IO.FileInfo' cannot be converted to 'System.IO.Stream'.

plus i see no where to pass the text i want to write to the file

any help?

thanks

mark
 
A

Armin Zingler

Am 11.06.2010 03:16, schrieb mp:
according to online elearning (ms)
to write to a debug log use the following:
'd. FileInfo fi = new FileInfo(@"c:\application.log");

' StreamWriter sw = fi.AppendText();

I can't seem to convert that to vb successfully

Dim fi As FileInfo = New FileInfo(m_DebugFileName)

Did you
import System.IO
?

'this didn't work

Dim sw As StreamWriter = New StreamWriter(fi.AppendText())


Dim sw As StreamWriter = fi.AppendText()

The AppendText function already creates a FileStream and returns
a StreamWriter writing into the stream. It's the same as

Dim sw As New StreamWriter(fi.FullName, True)


(I don't know why it's called "AppendText". You could append everything.)
'so i tried this and still no joy

Dim sw As StreamWriter = New StreamWriter(fi)

Dim sw As StreamWriter = New StreamWriter(fi.fullname)
Error 1 Overload resolution failed because no accessible 'New' can be called
with these arguments:
'Public Sub New(path As String)': Value of type 'System.IO.FileInfo'
cannot be converted to 'String'.
'Public Sub New(stream As System.IO.Stream)': Value of type
'System.IO.FileInfo' cannot be converted to 'System.IO.Stream'.


plus i see no where to pass the text i want to write to the file

any help?

I don't see it, too. ;-)

sw.writeline("yadda")


There are several ways to write to a file. For example:

Using fs = fi.Open(FileMode.Append, FileAccess.Write, FileShare.Read)
Using sw As New StreamWriter(fs, System.Text.Encoding.Default)
sw.WriteLine("abc")
End Using
End Using


Or, if you just want to open, write, close a/to a file:

File.AppendAllText(Path, "abc", System.Text.Encoding.Default)
 
M

mp

Armin Zingler said:
Am 11.06.2010 03:16, schrieb mp:

Did you
import System.IO
?

yes, otherwise the error would have been missing reference or something?

Dim sw As StreamWriter = fi.AppendText()

The AppendText function already creates a FileStream and returns
a StreamWriter writing into the stream. It's the same as

Dim sw As New StreamWriter(fi.FullName, True)


(I don't know why it's called "AppendText". You could append everything.)


Dim sw As StreamWriter = New StreamWriter(fi.fullname)


I don't see it, too. ;-)

sw.writeline("yadda")


There are several ways to write to a file. For example:

Using fs = fi.Open(FileMode.Append, FileAccess.Write,
FileShare.Read)
Using sw As New StreamWriter(fs, System.Text.Encoding.Default)
sw.WriteLine("abc")
End Using
End Using


Or, if you just want to open, write, close a/to a file:

File.AppendAllText(Path, "abc", System.Text.Encoding.Default)

Hi Armin,
That last line is how i've been doing it previously.
when the elearning lesson said it should be the other way i thought i'd try
that.

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



it said the correct answer was d.

ps, what is the meaning of @ in those arguments?



thanks

mark
 
M

mp

cuious what's the difference between that and just doing
Dim sw As StreamWriter = New StreamWriter(m_DebugFileName, True)

sw.WriteLine(strMsg)

sw.Close()

what is the FileInfo object doing other than passing the filename which you
already have?
 
A

Armin Zingler

Am 11.06.2010 05:08, schrieb mp:
cuious what's the difference between that and just doing

Dim sw As StreamWriter = New StreamWriter(m_DebugFileName, True)

sw.WriteLine(strMsg)

sw.Close()

what is the FileInfo object doing other than passing the filename which you
already have?

Nothing special. Just took it because you've mentioned it before. If
you don't need 'fi', just use the path (String).
 
A

Armin Zingler

Am 11.06.2010 04:44, schrieb mp:
yes, otherwise the error would have been missing reference or something?

yes, something. You didn't write what the problem was, so I guessed. :)

Hi Armin,
That last line is how i've been doing it previously.
when the elearning lesson said it should be the other way i thought i'd try
that.

Probably because the question was how to _create_ the log file, not how
to write into it. File.AppendAllText above opens, writes, closes the file.
For logging I'd prefer File.AppendAllText because, if the app crashes,
the write buffer hasn't been flushed to disk. Therefore some content
is missing, which is not helpful for logging, maybe for finding errors.
(but that was not the "e-learning question".)

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



it said the correct answer was d.


ps, what is the meaning of @ in those arguments?

If I enter it in the help index, it finds "@ (string literal)"
 

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

Top