Solved

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

Posted on 2008-09-30
12
1,063 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

15 Experts available now in Live!

Get 1:1 Help Now