Solved

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

Posted on 2008-09-30
12
1,073 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now