Sign in to follow this  
BeanDog

Windows--Correct file path case?

Recommended Posts

Filenames and paths are not case-sensitive under Windows. But they *are* stored one way or the other on the file system, when you get right down to it. If I have a filename in all lowercase, is there a way to get the correct case as held in the file system? I prefer a .NET example, but any help would be greatly appreciated. I've tried:
if(File.Exists(strPath))
{
	String strPathOnly = strPath.Substring(0, strPath.LastIndexOf('\\')+1);
	String strFilename = strPath.Substring(strPath.LastIndexOf('\\')+1);
	return(Directory.GetFiles(strPathOnly, strFilename)[0]);
}	
else
	return(strPath);
but it returns it in the case I gave it to the function. I tried a similar thing with the FileInfo class, but it again gave me back the same case I gave it. ~BenDilts( void );

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
did u try FindFile (Win32, don't know what the .NET equivalent is
or if you have to import it)?

DAP

Share this post


Link to post
Share on other sites
You want to use GetLongFileName. I don't know if it's got a .NET equivalent but here's some sample code from pinvoke.net:


[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern uint GetLongPathName(
string lpszShortPath,
[Out] StringBuilder lpszLongPath,
uint cchBuffer);


public static string ToLongPathName(string shortName)
{
StringBuilder longNameBuffer = new StringBuilder(256);
uint bufferSize = (uint)longNameBuffer.Capacity;
GetLongPathName(shortName, longNameBuffer, bufferSize);
return longNameBuffer.ToString();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this