Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-09-30
12
Medium Priority
?
1,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 200 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 1800 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

Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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