convert lowercase pathname to 'true casing'

G

Guest

Given a pathname that is all lowercase, what is the best way to recover the
"real pathname" as used by the filesystem.

This is for a Windows-explorer-like tool which has to store filenames in a
config file, and the "real paths" might have changed since the config files
were written.

For example:
Assuming the filesystem has a file called "C:\Test\test.file", how can I
perform the mapping:
"c:\test\test.file" => "C:\Test\test.file"
eg via
public string ConvertToRealPath(string path)

My current solution is to use win32APIs to perform "myName"->"windows short
name"->"windows long name", but there has to be a better way.

If I use GetDrives, Directory.GetFiles etc, the real casing is available,
but simply making a new DirectoryInfo("c:\test\test.file") does not recover
the real casing.

thanks,
Mike.
 
M

Morten Wennevik

Hi Mike,

I'm not aware of any framework classes capable of returning this info, nor
any win32 methods either, but shortname->longname isn't much coding at
all, although I agree with you that a single method is more elegant.

static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
GetShortPathName( @"c:\uppercasedir", sb, 1024);
GetLongPathName(sb.ToString(), sb, 1024);
}

[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string longpath, StringBuilder
sb, int buffer);

[DllImport("kernel32.dll")]
static extern uint GetLongPathName(string shortpath, StringBuilder
sb, int buffer);
 
G

Guest

Hi and thanks for the response. I looked for a while and didn't find
anything.. I was very suprised this wasn't available. The other fix is to
uppercase the drive name.. my current method, in its nastiness:

public static string RealPathName(string path)
{
try
{
StringBuilder shortPathBuilder = new StringBuilder(1024);
GetShortPathName(path, shortPathBuilder, 1024);
string shortname = shortPathBuilder.ToString();

StringBuilder longPathBuilder = new StringBuilder(1024);
GetLongPathName(shortname.ToString(), longPathBuilder, 1024);
string longname = longPathBuilder.ToString();

// and force uppercase on the Drive name
char firstchar = longname[0];
longname = firstchar.ToString().ToUpper() + longname.Substring(1);

if (longname.Length == path.Length)
result = longname.ToString();
}
catch
{
}

if (result == null)
return path;
else
return result;
}
 
M

Morten Wennevik

Hi Mike,

You can manipulate strings directly in a StringBuilder (and reuse the
builder) so your code could boil down to

try
{
StringBuilder sb = new StringBuilder();
GetShortPathName(path, sb, 1024);
GetLongPathName(sb.ToString(), sb, 1024);

sb[0] = Char.ToUpper(sb[0]);

return sb.ToString();
}
catch
{
return path;
}



Hi and thanks for the response. I looked for a while and didn't find
anything.. I was very suprised this wasn't available. The other fix is
to
uppercase the drive name.. my current method, in its nastiness:

public static string RealPathName(string path)
{
try
{
StringBuilder shortPathBuilder = new StringBuilder(1024);
GetShortPathName(path, shortPathBuilder, 1024);
string shortname = shortPathBuilder.ToString();

StringBuilder longPathBuilder = new StringBuilder(1024);
GetLongPathName(shortname.ToString(), longPathBuilder, 1024);
string longname = longPathBuilder.ToString();

// and force uppercase on the Drive name
char firstchar = longname[0];
longname = firstchar.ToString().ToUpper()
+ longname.Substring(1);
if (longname.Length == path.Length)
result = longname.ToString();
}
catch
{
}

if (result == null)
return path;
else
return result;
}

Morten Wennevik said:
Hi Mike,

I'm not aware of any framework classes capable of returning this info,
nor
any win32 methods either, but shortname->longname isn't much coding at
all, although I agree with you that a single method is more elegant.

static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
GetShortPathName( @"c:\uppercasedir", sb, 1024);
GetLongPathName(sb.ToString(), sb, 1024);
}

[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string longpath,
StringBuilder
sb, int buffer);

[DllImport("kernel32.dll")]
static extern uint GetLongPathName(string shortpath,
StringBuilder
sb, int buffer);
 

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