自
以来,是否可以设置区分大小写的Directory.Exists
/ File.Exists
Directory.Exists(folderPath)
和
Directory.Exists(folderPath.ToLower())
都返回true
?
大部分时间没关系,但我使用的宏如果路径与100%的情况不匹配似乎不起作用。
答案 0 :(得分:5)
由于Directory.Exists使用的FindFirstFile不区分大小写,因此不会。但是,您可以PInvoke FindFirstFileEx将additionalFlags参数设置为FIND_FIRST_EX_CASE_SENSITIVE
答案 1 :(得分:1)
尝试此功能:
public static bool FileExistsCaseSensitive(string filename)
{
string name = Path.GetDirectoryName(filename);
return name != null
&& Array.Exists(Directory.GetFiles(name), s => s == Path.GetFullPath(filename));
}
<强> 更新 强>
如评论中所述,这仅检查文件名中的案例,而不是路径中的案例。这是因为GetFullPath方法不会返回原始案例的Windows原始路径,而是返回参数路径的副本。
例如:
GetFullPath("c:\TEST\file.txt") -> "c:\TEST\file.txt"
GetFullPath("c:\test\file.txt") -> "c:\test\file.txt"
我尝试的所有方法都以相同的方式工作:Fileinfo,DirectoryInfo。
以下是使用kernel32.dll方法的解决方案:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetLongPathName(
string path,
StringBuilder longPath,
int longPathLength
);
/// <summary>
/// Return true if file exists. Non case sensitive by default.
/// </summary>
/// <param name="filename"></param>
/// <param name="caseSensitive"></param>
/// <returns></returns>
public static bool FileExists(string filename, bool caseSensitive = false)
{
if (!File.Exists(filename))
{
return false;
}
if (!caseSensitive)
{
return true;
}
//check case
StringBuilder longPath = new StringBuilder(255);
GetLongPathName(Path.GetFullPath(filename), longPath, longPath.Capacity);
string realPath = Path.GetDirectoryName(longPath.ToString());
return Array.Exists(Directory.GetFiles(realPath), s => s == filename);
}
答案 2 :(得分:0)
尝试这两个更简单的选项,不需要使用PInvoke并返回一个可以为空的布尔值(bool?)。我不是专家,所以我知道这是否是最有效的代码,但它对我有用。
只需传入一个路径,如果结果为null(HasValue = false),则找不到匹配项,如果结果为false,则表示存在完全匹配,否则如果为true则匹配差异情况。
方法GetFiles,GetDirectories和GetDrives都返回保存在文件系统中的确切大小写,因此您可以使用区分大小写的比较方法。
注意:对于路径是精确驱动器的情况(例如@&#34; C:\&#34;),我必须使用稍微不同的方法。
using System.IO;
class MyFolderFileHelper {
public static bool? FileExistsWithDifferentCase(string fileName)
{
bool? result = null;
if (File.Exists(fileName))
{
result = false;
string directory = Path.GetDirectoryName(fileName);
string fileTitle = Path.GetFileName(fileName);
string[] files = Directory.GetFiles(directory, fileTitle);
if (String.Compare(files[0], fileName, false) != 0)
result = true;
}
return result;
}
public static bool? DirectoryExistsWithDifferentCase(string directoryName)
{
bool? result = null;
if (Directory.Exists(directoryName))
{
result = false;
directoryName = directoryName.TrimEnd(Path.DirectorySeparatorChar);
int lastPathSeparatorIndex = directoryName.LastIndexOf(Path.DirectorySeparatorChar);
if (lastPathSeparatorIndex >= 0)
{
string baseDirectory = directoryName.Substring(lastPathSeparatorIndex + 1);
string parentDirectory = directoryName.Substring(0, lastPathSeparatorIndex);
string[] directories = Directory.GetDirectories(parentDirectory, baseDirectory);
if (String.Compare(directories[0], directoryName, false) != 0)
result = true;
}
else
{
//if directory is a drive
directoryName += Path.DirectorySeparatorChar.ToString();
DriveInfo[] drives = DriveInfo.GetDrives();
foreach(DriveInfo driveInfo in drives)
{
if (String.Compare(driveInfo.Name, directoryName, true) == 0)
{
if (String.Compare(driveInfo.Name, directoryName, false) != 0)
result = true;
break;
}
}
}
}
return result;
}
}
答案 3 :(得分:0)
如果文件的(相对或绝对)路径为:
string AssetPath = "...";
以下内容可确保文件存在并具有正确的大小写:
if(File.Exists(AssetPath) && Path.GetFullPath(AssetPath) == Directory.GetFiles(Path.GetDirectoryName(Path.GetFullPath(AssetPath)), Path.GetFileName(Path.GetFullPath(AssetPath))).Single())
{
}
享受!
答案 4 :(得分:0)
这里有一个相对简单的方法来检查一个目录是否确实存在。我需要这个,因为我有一个重命名文件夹的选项,它首先检查不会有冲突。因此,例如,如果我想将文件夹“cars”重命名为“Cars”。
这真的很直接。如果系统报告该文件夹存在,那么我只需在父文件夹上调用 GetDirectories,并将目录名称作为通配符传递(因此正好返回 1 个结果)。只是一个简单的比较就给了我答案。
static public bool DirExistsMatchCase(string path)
{
// If it definitely doesn't return false
if (!Directory.Exists(path)) return false;
// Figure out if the case (of the final part) is the same
string thisDir = Path.GetFileName(path);
string actualDir = Path.GetFileName(Directory.GetDirectories(Path.GetDirectoryName(path), thisDir)[0]);
return thisDir == actualDir;
}