Results from fileinfo - how to return, case-sensitive

N

Nathan

I'm getting results from Indexing Service and the problem is that all the
results are returned in lowercase; if a file has capital letters in either
its name or its path, Indexing Service doesn't return the file this way,
thus the file path cannot be used in a link (since http *is*
case-sensitive).

In the following code line:
Dim fi as new fileinfo(<path>)

If path is all in uppercase, the path (or .fullname) is returned all in
uppercase - the same happens if the path is all in lowercase.

Is there anyway to get a file's path in its *original* case, i.e. the path
you see in the address bar of Windows Explorer?
 
N

Nathan

Maybe I should re-think my question...

If a file's path is this: C:\Test DOCUMENT\Test.html

and I have the string: c:\test document\test.html

How do I get the file's original path (i.e. with the case exactly as it is)?
 
M

Martin Dechev

Please, don't ask me to write this in VB.

using System;
using System.IO;

public class Class1
{

static void Main()
{
string file =
@"C:\Program Files\Microsoft Visual Studio .NET
2003\VC#\CSharpProjects\CSharpConsole.vsz".ToLower();
string dir = Path.GetDirectoryName(file);
dir = ReplaceDirsWithExactCase(
dir,
Path.GetPathRoot(dir).ToUpper());
string filename =
Path.GetFileName(GetExactCaseForFilename(file));
Console.WriteLine(
"{0}: ->{1}{1}Path: {2}{1}File: {3}{1}{1}Exact case:{1}{2}\\{3}",
file,
Environment.NewLine,
dir,
filename);
Console.ReadLine();
}

public static string ReplaceDirsWithExactCase(string fullpath, string
parent)
{
if(fullpath.LastIndexOf(@"\") != fullpath.Length - 1)
fullpath += @"\";
if(parent.LastIndexOf(@"\") != parent.Length - 1)
parent += @"\";
string lookfor =
fullpath.ToLower().Replace(parent.ToLower(), "");
lookfor =
(parent + lookfor.Substring(0,
lookfor.IndexOf(@"\"))).ToLower();
string[] dirs =
Directory.GetDirectories(parent);
foreach(string dir in dirs)
if(dir.ToLower() == lookfor)
{
if(lookfor + @"\" == fullpath.ToLower())
return dir;
else
return ReplaceDirsWithExactCase(fullpath, dir);
}
return null;
}

public static string GetExactCaseForFilename(string file)
{
string[] files =
Directory.GetFiles(Path.GetDirectoryName(file));
foreach(string f in files)
if(f.ToLower() == file.ToLower())
return f;
return null;
}
}

Greetings
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top