Solved

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

Posted on 2008-09-30
12
1,109 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 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