Complete path name?

  • Thread starter Thread starter Hubert Hermanutz
  • Start date Start date
H

Hubert Hermanutz

Hello,

I use an initialized "System.IO.FileInfo" class. The properties
"myFileInfo.FullName" or "myFileInfo.DirectoryName" deliver the directory
with MS-DOS convention.
Example: "c:\docume~1\...".

How can i get the full Path.
Example: "c:\documents\..."

Thanks in advance,
Hubert
 
Using only built-in functionality, you cant. The problem is that whenever
you call the DirectoryName (or any System.IO.FileInfo or
System.IO.DirectoryInfo Name or FullName property) you get the name exactly
as you spelled it while calling the property. FileInfo and DirectoryInfo
never check whether actual file or directory exist while calling
DirectoryName (or Name or FullName) properties, they simply read it from
string you specified while creating the FileInfo or DirectoryInfo instances.

In order to get the correct full name you have to write your own method
which could look like this one:

private string DirectoryFullName(System.IO.DirectoryInfo dir)
{
string dirName = "";
while (dir.Parent != null)
{
string thisDir = dir.GetDirectories(".")[0].Name;
dirName = thisDir + (dirName != "" ? "\\" : "") + dirName;
dir = dir.Parent;
}
return dir.Root + dirName;
}

The trick is to call GetDirectories specifying the current directory (.) as
search pattern. The DirectoryInfo array you get will contain the list of
actual directories as they exist in the filesystem. Repeating the process as
long as there are parent directories will give you exactly the result you
need.

Regards,
 
Back
Top