How to get the FileName and Path to which a StreamWriter is attached to?

  • Thread starter Thread starter José Joye
  • Start date Start date
J

José Joye

I have a instance of a StreamWriter and I need to get the path and filename
to which it is attached to.
This sounds easy....
I feel a bit idiot not finding it!

José
 
José Joye said:
I have a instance of a StreamWriter and I need to get the path and filename
to which it is attached to.
This sounds easy....
I feel a bit idiot not finding it!

There's no guarantee that it's writing to a file in the first place. It
could be writing directly into a MemoryStream, for instance. You could
use StreamWriter.BaseStream to get the stream it's writing to, and if
that's a FileStream, cast it to that and look at the Name property. Why
do you need to know though?
 
Thanks!

You're right.
I will use the 'is' keyword to really see if it is a FileStream and if yes,
I will cast it and use the 'Name' property.

In fact, I need to know it because I have a 'TextWriterTraceListener' that
is used for logging purpose. I want to implement a timer that will remove
it from the Tracing Collection, flush it, close it and rename the file with
a timestamp. It will then create a fresh file (with inital name) and add it
again to the Tracing collection.
In this way, I want to implement a 'kind of generic' log file versioning in
my logging class used by several applications.

José

José Joye said:
I have a instance of a StreamWriter and I need to get the path and filename
to which it is attached to.
This sounds easy....
I feel a bit idiot not finding it!

There's no guarantee that it's writing to a file in the first place. It
could be writing directly into a MemoryStream, for instance. You could
use StreamWriter.BaseStream to get the stream it's writing to, and if
that's a FileStream, cast it to that and look at the Name property. Why
do you need to know though?
 
José Joye said:
You're right.
I will use the 'is' keyword to really see if it is a FileStream and if yes,
I will cast it and use the 'Name' property.

In fact, I need to know it because I have a 'TextWriterTraceListener' that
is used for logging purpose. I want to implement a timer that will remove
it from the Tracing Collection, flush it, close it and rename the file with
a timestamp. It will then create a fresh file (with inital name) and add it
again to the Tracing collection.
In this way, I want to implement a 'kind of generic' log file versioning in
my logging class used by several applications.

Why not just set the Name of the TextWriterTraceListener to be the name
of the log file?
 
In fact, I use from my app config file the following:

<add name="BTListener"

type="System.Diagnostics.TextWriterTraceListener,System, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"

initializeData="C:\temp\TestTracing.log" />

<remove type="System.Diagnostics.DefaultTraceListener,System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />



I want to allow my class clients to use this feature on the 'BTListener'
TextWriterTraceListener. I will not be aware of the file to which it is
attached to. This is the reason I asked the question.

José



José Joye said:
You're right.
I will use the 'is' keyword to really see if it is a FileStream and if yes,
I will cast it and use the 'Name' property.

In fact, I need to know it because I have a 'TextWriterTraceListener' that
is used for logging purpose. I want to implement a timer that will remove
it from the Tracing Collection, flush it, close it and rename the file with
a timestamp. It will then create a fresh file (with inital name) and add it
again to the Tracing collection.
In this way, I want to implement a 'kind of generic' log file versioning in
my logging class used by several applications.

Why not just set the Name of the TextWriterTraceListener to be the name
of the log file?
 
Jose,

I think that you are going about this the wrong way. Instead of
creating a timer that will remove the trace listener (which you shouldn't do
unless it is your own), why not derive a class from TraceListener? In this
class, you would have the timer with a callback. When the timer is fired,
you would create an instance of the TextWriterTraceListener and hold it
internally. Then, you would just aggregate the calls, making sure to lock
on an object so that you don't try and make a call on a null reference.

Hope this helps.
 
Back
Top