Solved

Get canonical filename - turn C:\diR\fiLe.Txt into C:\Dir\File.txt

Posted on 2008-09-30
12
1,079 Views
Last Modified: 2013-12-17
In C#, I need to turn a full pathname which is valid but possibly of the wrong case into the "correct" pathname.  So for example, there's a file called C:\Dir\File.txt but the pathname I have is C:\diR\fiLe.Txt.  (I know the "wrong" pathname will work, but I still need the right one, as displayed by Windows Explorer).
0
Comment
Question by:RichieHindle
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 3

Expert Comment

by:bmilli
ID: 22609466

This works in VB.Net.  Not sure what you would need to do other than add ; at the end for C#.  sVal contains c:\patH\fiLe.txt before the call and C:\Path\Ffile.Txt after the call.

sVal = StrConv(sVal, VbStrConv.ProperCase)
0
 
LVL 3

Expert Comment

by:bmilli
ID: 22609468

sorry... C:\Path\File.Txt after the call.
0
 
LVL 14

Author Comment

by:RichieHindle
ID: 22609569
bmilli: Sorry, I didn't make myself clear.  I want the true pathname, in whatever case it is on the disk.  So if the file is called C:\MyDirectory\MyFileWithAMixedCaseName.txt, that's the pathname I want.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 22609575
Try some System.IO magic with the Path and Directory classes (just for fun) :D


using System.IO;
 
            string filePath = @"C:\TeMP\REFUNDS.CSV";
 
            string directoryName = Path.GetDirectoryName(filePath);
            string fileName = Path.GetFileName(filePath);
 
            string realDirectory = Directory.GetDirectories(Path.GetDirectoryName(directoryName), Path.GetFileName(directoryName))[0];
            string realFileName = Path.GetFileName(Directory.GetFiles(directoryName, fileName)[0]);
 
            string realPath = Path.Combine(realDirectory, realFileName);

Open in new window

0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 total points
ID: 22609589
use this:

string fn = @"C:\Dir\File.txt";
string goodFn;
try
{
    goodFn = Directory.GetFiles(Path.GetDirectoryName(fn), Path.GetFilename(fn))[0];
}
catch
{
    goodFn = null;
}
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22609693
Does your solution need to be cross platform? All of these solutions rely on the windows file system and its case insensitivty. Most would fail on another platform (like through mono).

Cheers,

Greg
0
 
LVL 14

Author Comment

by:RichieHindle
ID: 22609887
gregoryyoung: It only needs to work on Windows.

jaime_olivares / TheLearnedOne: It needs to work with any length of pathname, eg.: C:\proJECTs\mYProjecT\wEb\cOntent\inDex.hTMl
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22609904
jaime's and I believe Bob's does work with any legal length path name.
0
 
LVL 14

Author Comment

by:RichieHindle
ID: 22609933
gregoryyoung: jaime's code doesn't correct the case of the directory names, only the filename.  Here's a transcript from Visual Studio's Immediate Window:

string fn = @"C:\sRc\wEb\cOntent\wOrkflow.hTMl";
"C:\\sRc\\wEb\\cOntent\\wOrkflow.hTMl"
Directory.GetFiles(Path.GetDirectoryName(fn), Path.GetFileName(fn))[0];
"C:\\sRc\\wEb\\cOntent\\workflow.html"
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22609987
oh when you said "work with any length of path name" I took it to mean any level of path name ...

to get the appropriate directory name you can just use the DirectoryInfo class

Cheers,

Greg
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 450 total points
ID: 22610912
Try this:


using System;
using System.Text;
using System.IO;
 
public class CanonicalPath
{
 
    public static string MakeCanonical(string filePath)
    {
        if (!File.Exists(filePath))
            throw new FileNotFoundException();
 
        string directoryName = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(Directory.GetFiles(directoryName, Path.GetFileName(filePath))[0]);
        string[] folderList = directoryName.Split('\\');
 
        StringBuilder fullPath = new StringBuilder(filePath.Length);
 
        foreach (string folder in folderList)
        {
            if (!folder.EndsWith(":"))
            {
                string canonicalPath = Path.GetFileName(Directory.GetDirectories(fullPath.ToString(), folder)[0]);
 
                fullPath.Append(canonicalPath + "\\");
            }
            else
            {
                fullPath.Append(folder.ToUpper() + "\\");
            }
        }
 
        return Path.Combine(fullPath.ToString(), fileName);
    }
 
}

Open in new window

0
 
LVL 14

Author Closing Comment

by:RichieHindle
ID: 31501733
Thanks!  I love the fact that it uppercases the drive name - nice touch. 8-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question