C# 文件路径重命名
我正在尝试用 C# 编写一个静态成员函数,或者在 .NET Framework 中找到一个静态成员函数,该函数会将文件路径重新设置为文件系统指定的内容。
示例:
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
我已经尝试了下面的代码及其许多变体,但它仍然不起作用。 我知道 Windows 通常不区分大小写,但我需要将这些文件路径传递给 ClearCase,ClearCase 会考虑文件路径大小写,因为它是 Unix 和 Windows 应用程序。
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个非常简单的实现,假设文件和目录都存在并且可以访问:
但是,这有一个错误:相对路径转换为绝对路径。 上面的原始代码做了同样的事情,所以我假设您确实想要这种行为。
This is a pretty simple implementation that assumes that the file and directories all exist and are accessible:
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.
上面 @Ants 的答案绝对应该被视为已接受的答案。 不过,我根据自己的目的对其进行了一些重构。 该方法被打包为 FileInfo 和 DirectoryInfo 的扩展方法,并返回更正后的方法。
我一直在为 FileInfo 的一些大小写不一致问题而苦恼。 为了确保鲁棒性,我在比较或存储路径时转换为全部大写。 为了阐明代码的意图,我还有这些扩展方法:
The answer by @Ants above should absolutely get credit as the accepted answer. However, I refactored it a bit to my purposes. The approach is packaged as extension methods for FileInfo and DirectoryInfo, and return corrected ones as well.
I've been banging my head over some case-inconsistency issues with FileInfo. In order to ensure robustness, I convert to all caps when doing comparison or storage of the paths. To clarify the intent of the code, I also have these extension methods:
下面的内容在我测试的范围内运行良好...唯一的问题是所使用的 API 仅在 Vista 中可用。
The below works fine to the extent I tested... only catch is that the API used is available only in Vista.
您可以搜索想要获取大小写的文件并返回搜索结果(您想要检查存在的文件的大小写,对吧?)。 像这样的事情:
这是你要找的吗?
You can search for the file you want to get the case on and return the results of your search (you want to check the casing of a file that exists, right?). Something like this:
Is this what you're looking for?
您将希望系统为您找到该文件。 我通过假装我不知道确切的路径来做到这一点,即让系统搜索:
因此我们在目录中搜索,过滤确切的文件名并将搜索限制为仅当前目录(没有递归)。
这将返回一个字符串数组,其中包含大小写正确的单个文件路径(如果文件存在)或不包含任何内容(如果文件不存在)。
一个警告:您可能需要禁止输入路径中使用通配符,因为此方法接受它们并可能因此找到多个文件。
编辑
驱动器号似乎仍遵循我们提供的大小写。 此外,还需要针对 UNC 路径进行测试。
You'll want the system to find the file for you. I do this by pretending that I do not know the exact path, i.e. have the system search:
So we search in the directory, filtering on the exact file name and limiting the search to the current directory only (no recursion).
This returns a string array containing either the single file path with correct casing (if the file exists) or nothing (if the file does not exist).
One warning: You may need to disallow wildcards in the input path, because this approach accepts them and may find multiple files as a result.
Edit
The drive letter appears to still follow the casing that we provide. Also, this needs to be tested for UNC paths.
我有一些更有效的方法,但是:
1)它似乎并不适用于所有情况。 (我还没有弄清楚哪些文件和目录正确获得大小写,哪些文件和目录没有获得大小写的模式。)
2)它是 Windows 特定的。
I have something more efficient but:
1) It doesn't seem to work for all cases. (I've not figured out the pattern of which files and directories it correctly gets the casing, and which ones it does not.)
2) It's Windows specific.