Home All Groups Group Topic Archive Search About

elearning info not working

Author
11 Jun 2010 1:16 AM
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

Author
11 Jun 2010 2:15 AM
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)


--
Armin
Author
11 Jun 2010 2:44 AM
mp
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:OLzJ2wQCLHA.3880@TK2MSFTNGP04.phx.gbl...
> 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
> ?

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


Show quoteHide quote
>
>
>> '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)
>
>
> --
> Armin

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
Author
11 Jun 2010 3:08 AM
mp
Show quote Hide quote
"mp" <nospam@thanks.com> wrote in message
news:hus7uf$7ht$1@news.eternal-september.org...
>
> "Armin Zingler" <az.nospam@freenet.de> wrote in message
> news:OLzJ2wQCLHA.3880@TK2MSFTNGP04.phx.gbl...
>> 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");
SNIP
>>
>> 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
>>

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?
Author
11 Jun 2010 11:55 AM
Armin Zingler
Am 11.06.2010 05:08, schrieb mp:
Show quoteHide quote
>>> 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
>>>
>
> 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).


--
Armin
Author
11 Jun 2010 12:09 PM
Armin Zingler
Am 11.06.2010 04:44, schrieb mp:
Show quoteHide quote
>>> 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
>> ?
>
> yes, otherwise the error would have been missing reference or something?

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


>> 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.

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".)


Show quoteHide quote
> '
> '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)"


--
Armin