Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full path of StreamWriter

I'm creating a StreamWriter using a relative path. But the file doesn't appear. To troubleshoot, I want to check that the full path is what I'm expecting. So having a StreamWriter instance, how can I get the full path of the file it's going to write to?

string fileName = "relative/path.txt"
StreamWriter sw= new StreamWriter(fileName);
// What is the full path of 'sw'?
like image 593
Oleg Vazhnev Avatar asked May 07 '12 13:05

Oleg Vazhnev


2 Answers

In my version of the framework, this seems to work:

string fullPath = ((FileStream)(streamWriter.BaseStream)).Name;

(Found by debugging.)

like image 58
Jeppe Stig Nielsen Avatar answered Sep 22 '22 14:09

Jeppe Stig Nielsen


To get the full path from a relative path, use the Path.GetFullPath method.

For example:

string fileName = "relative/path.txt";
string fullPath = Path.GetFullPath(fileName);
like image 22
Steve Avatar answered Sep 25 '22 14:09

Steve