C#中的文件路径比较已经有question了。但提供的解决方案意味着我有绝对的路径。任何人都可以为相对路径提出一个很好的解决方案,或指出在比较路径时我必须注意的事情(在Windows上)。
例如:
share/logs
share\logs
share/logs\
这些字符串意味着相同的路径
答案 0 :(得分:3)
您在帖子中链接的答案应该对您有用。 GetFullPath
不仅可以解析绝对路径的完整路径,还可以解析绝对路径的相对路径。
只是不要忘记使用链接答案中提供的代码来解决尾部斜杠并添加代码以将/
替换为\
(如Henk所述)
答案 1 :(得分:2)
正如Henk指出的那样,在某些情况下可能必须首先清理路径(或拒绝作为有效路径)。 This页面描述了有效的文件和路径名称。您可能希望在使用以下内容进行比较之前清理路径:
string FormatPath(string path){
string result = path.Replace("/","\\");
result = result.TrimStart("\\");
return result;
}
通过查看DirectoryInfo API参考,以下内容应该有效:
DirectoryInfo d1 = new DirectoryInfo(FormatPath("share/logs")));
DirectoryInfo d2 = new DirectoryInfo(FormatPath("share\logs")));
if(d1.FullName.Equals(d2.FullName)){
// They are the same
}
基本上只是从相对路径中提取绝对路径,并比较绝对路径。
答案 2 :(得分:2)
我认为不是真正理解你的问题,但是:
你将如何比较它们?
系统中的所有路径都具有相同的唯一根,没有子路径。只是比较可比较的路径字符串是否相等(在路径格式之前统一,我的意思是'/''\'符号)。不太好的解决方案,因为有一天你或经理可能希望有嵌套的路径,你的解决方案将会制动。
只需找出完整路径并按照链接中提供的解决方案进行操作。
答案 3 :(得分:1)
您应该尝试此问题中提出的解决方案:How can I compare (directory) paths in C#?
Path.GetFullPath()可以将相对路径作为参数:http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
答案 4 :(得分:1)
这段代码规范化路径(包括相对路径)。一旦路径被规范化,(不区分大小写)字符串比较就等同于路径比较。
这个特定的实现并不假设“/”等于“\”,但您可以通过在将字符串传递给此方法之前替换“/”来轻松解决这个问题....
/// <summary>
/// Converts a path in a form suitable for comparison with other paths.
/// </summary>
/// <remarks>
/// <para>
/// In general case, two equivalent paths do not necessarily have the same string
/// representation. However, after subjecting them to this method, they will have
/// (case-insensitively) equal string representations.
/// </para>
/// <para>
/// Removes ".." and "." and always trims trailing path separator (except for paths
/// in format "X:\" or "\"). Does not change case.
/// </para>
/// <para>
/// This method does not attempt to validate the path (since its purpose is only to
/// make paths comparable as strings), so some logically incorrect paths will pass
/// through it unscathed. Examples of such paths include: "C:\..", "\..",
/// "\\machine\c$\..", "\\machine\c$\..\.." etc...
/// </para>
/// </remarks>
/// <returns>
/// Normalized path. Empty or <c>null</c> <paramref name="path"/> results in empty or
/// <c>null</c> result.
/// </returns>
/// <seealso cref="PathComparer"/>
public static string NormalizePath(string path) {
if (string.IsNullOrEmpty(path))
return path;
// Remove path root.
string path_root = Path.GetPathRoot(path);
path = path.Substring(path_root.Length);
string[] path_components = path.Split(Path.DirectorySeparatorChar);
// "Operating memory" for construction of normalized path.
// Top element is the last path component. Bottom of the stack is first path component.
Stack<string> stack = new Stack<string>(path_components.Length);
foreach (string path_component in path_components) {
if (path_component.Length == 0)
continue;
if (path_component == ".")
continue;
if (path_component == ".." && stack.Count > 0 && stack.Peek() != "..") {
stack.Pop();
continue;
}
stack.Push(path_component);
}
string result = string.Join(new string(Path.DirectorySeparatorChar, 1), stack.Reverse().ToArray());
result = Path.Combine(path_root, result);
return result;
}
答案 5 :(得分:0)