Connecting Tech Pros Worldwide Help | Site Map

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

José Joye
Guest
 
Posts: n/a
#1: Nov 15 '05
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é


Jon Skeet
Guest
 
Posts: n/a
#2: Nov 15 '05

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


José Joye <jose.joye@KILLTHESPAMSbluewin.ch> wrote:[color=blue]
> 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![/color]

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?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
José Joye
Guest
 
Posts: n/a
#3: Nov 15 '05

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


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é

"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19d2409f96fdce769896ce@news.microsoft.com ...
José Joye <jose.joye@KILLTHESPAMSbluewin.ch> wrote:[color=blue]
> I have a instance of a StreamWriter and I need to get the path and[/color]
filename[color=blue]
> to which it is attached to.
> This sounds easy....
> I feel a bit idiot not finding it![/color]

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?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon Skeet
Guest
 
Posts: n/a
#4: Nov 15 '05

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


José Joye <jose.joye@KILLTHESPAMSbluewin.ch> wrote:[color=blue]
> 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.[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
José Joye
Guest
 
Posts: n/a
#5: Nov 15 '05

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


In fact, I use from my app config file the following:

<add name="BTListener"

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

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

<remove type="System.Diagnostics.DefaultTraceListener,Syst em,
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é



"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19d245343b2b36a09896cf@news.microsoft.com ...
José Joye <jose.joye@KILLTHESPAMSbluewin.ch> wrote:[color=blue]
> You're right.
> I will use the 'is' keyword to really see if it is a FileStream and if[/color]
yes,[color=blue]
> 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[/color]
with[color=blue]
> a timestamp. It will then create a fresh file (with inital name) and add[/color]
it[color=blue]
> again to the Tracing collection.
> In this way, I want to implement a 'kind of generic' log file versioning[/color]
in[color=blue]
> my logging class used by several applications.[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#6: Nov 15 '05

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


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.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com


"José Joye" <jose.joye@KILLTHESPAMSbluewin.ch> wrote in message
news:%23EoAnYQfDHA.2748@TK2MSFTNGP11.phx.gbl...[color=blue]
> In fact, I use from my app config file the following:
>
> <add name="BTListener"
>
> type="System.Diagnostics.TextWriterTraceListener,S ystem,[/color]
Version=1.0.5000.0,[color=blue]
> Culture=neutral, PublicKeyToken=b77a5c561934e089"
>
> initializeData="C:\temp\TestTracing.log" />
>
> <remove type="System.Diagnostics.DefaultTraceListener,Syst em,
> 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é
>
>
>
> "Jon Skeet" <skeet@pobox.com> wrote in message
> news:MPG.19d245343b2b36a09896cf@news.microsoft.com ...
> José Joye <jose.joye@KILLTHESPAMSbluewin.ch> wrote:[color=green]
> > You're right.
> > I will use the 'is' keyword to really see if it is a FileStream and if[/color]
> yes,[color=green]
> > I will cast it and use the 'Name' property.
> >
> > In fact, I need to know it because I have a 'TextWriterTraceListener'[/color][/color]
that[color=blue][color=green]
> > is used for logging purpose. I want to implement a timer that will[/color][/color]
remove[color=blue][color=green]
> > it from the Tracing Collection, flush it, close it and rename the file[/color]
> with[color=green]
> > a timestamp. It will then create a fresh file (with inital name) and add[/color]
> it[color=green]
> > again to the Tracing collection.
> > In this way, I want to implement a 'kind of generic' log file versioning[/color]
> in[color=green]
> > my logging class used by several applications.[/color]
>
> Why not just set the Name of the TextWriterTraceListener to be the name
> of the log file?
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>
>[/color]


Closed Thread