我试图写一个静态成员函数的C#或找到一个在.NET Framework将重新情况下,文件路径是什么文件系统指定。
例如:
字符串的文件路径= @C:\ TEMP.TXT;
文件路径= FileUtility.RecaseFilepath(文件路径);
//文件路径= C:\ TEMP.TXT
//凡在NTFS卷真正的完全合格的文件路径为C:\ TEMP.TXT
我试过下面的下面的code和它的许多变种,它仍然无法正常工作。 我知道Windows是不区分大小写的一般,但我需要通过这些文件路径的ClearCase它参考的文件路径外壳,因为它是一个Unix和Windows应用程序。
公共静态字符串GetProperFilePathCapitalization(字符串文件路径)
{
字符串结果=;
尝试
{
结果= Path.GetFullPath(文件路径);
DirectoryInfo的DIR =新的DirectoryInfo(Path.GetDirectoryName(结果));
的FileInfo [] F1 = dir.GetFiles(Path.GetFileName(结果));
如果(fi.Length大于0)
{
结果=网络连接[0] .FullName;
}
}
赶上(例外)
{
结果=文件路径;
}
返回结果;
}
解决方案
这是一个pretty的简单实现,假定该文件和目录都存在且可访问:
静态字符串GetProperDirectoryCapitalization(DirectoryInfo的dirInfo)
{
DirectoryInfo的parentDirInfo = dirInfo.Parent;
如果(空== parentDirInfo)
返回dirInfo.Name;
返回Path.Combine(GetProperDirectoryCapitalization(parentDirInfo)
parentDirInfo.GetDirectories(dirInfo.Name)[0] .Name点);
}
静态的字符串GetProperFilePathCapitalization(字符串文件名)
{
FileInfo的的fileInfo =新的FileInfo(文件名);
DirectoryInfo的dirInfo = fileInfo.Directory;
返回Path.Combine(GetProperDirectoryCapitalization(dirInfo)
dirInfo.GetFiles(fileInfo.Name)[0] .Name点);
}
没有与此错误,虽然:相对路径转换为绝对路径。以上你原来的code也一样,所以我假设你想要这种行为。
I'm trying to write a static member function in C# or find one in the .NET Framework that will re-case a file path to what the filesystem specifies.
Example:
string filepath = @"C:\temp.txt";
filepath = FileUtility.RecaseFilepath(filepath);
// filepath = C:\Temp.TXT
// Where the real fully qualified filepath in the NTFS volume is C:\Temp.TXT
I've tried the following code below and many variants of it and it still doesn't work. I know Windows is case-insensitive in general but I need to pass these file paths to ClearCase which considers file path casing since it's a Unix and Windows application.
public static string GetProperFilePathCapitalization(string filepath)
{
string result = "";
try
{
result = Path.GetFullPath(filepath);
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(result));
FileInfo[] fi = dir.GetFiles(Path.GetFileName(result));
if (fi.Length > 0)
{
result = fi[0].FullName;
}
}
catch (Exception)
{
result = filepath;
}
return result;
}
解决方案
This is a pretty simple implementation that assumes that the file and directories all exist and are accessible:
static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo)
{
DirectoryInfo parentDirInfo = dirInfo.Parent;
if (null == parentDirInfo)
return dirInfo.Name;
return Path.Combine(GetProperDirectoryCapitalization(parentDirInfo),
parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
}
static string GetProperFilePathCapitalization(string filename)
{
FileInfo fileInfo = new FileInfo(filename);
DirectoryInfo dirInfo = fileInfo.Directory;
return Path.Combine(GetProperDirectoryCapitalization(dirInfo),
dirInfo.GetFiles(fileInfo.Name)[0].Name);
}
There is a bug with this, though: Relative paths are converted to absolute paths. Your original code above did the same, so I'm assuming that you do want this behavior.