gpt4 book ai didi

c# - 为什么函数输出错误的值?

转载 作者:太空宇宙 更新时间:2023-11-03 19:06:46 25 4
gpt4 key购买 nike

下面我写的 Function 用于检查 File/Directory 路径是否存在,旁边还有 RecentPath 检索最后一个路径已由 Function 检查。

    private static String IRecentPath;
public static String RecentPath
{
get
{
return IRecentPath;
}
}

public static Boolean Exists(String Path, Int32 PathType = 0)
{
return Exist(Path, PathType);
}

internal static Boolean Exist(String Path, Int32 PathType = 0)
{
Boolean Report = false;
switch (PathType)
{
case 0:
Report = (Directory.Exists(Path) || File.Exists(Path));
IRecentPath = Path;
break;
case 1:
String MPath = AppDomain.CurrentDomain.BaseDirectory;
Report = (Directory.Exists(System.IO.Path.Combine(MPath, Path)) || File.Exists(System.IO.Path.Combine(MPath, Path)));
IRecentPath = System.IO.Path.Combine(MPath, Path);
break;
case 2:
String LPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Report = (Directory.Exists(System.IO.Path.Combine(LPath, Path)) || File.Exists(System.IO.Path.Combine(LPath, Path)));
IRecentPath = System.IO.Path.Combine(LPath, Path);
break;
default:
break;
}
return Report;
}

问题是 RecentPath 总是检索调用函数时设置的路径,而不是最终路径。

例子:

假设我需要检查 myDocument 中是否存在 /user 目录,然后获取最近检查过的路径,所以:

Path.Exists("/user", 2);
MessageBox.Show(Path.RecentPath);

输出应该是 C:\Users\Hossam\Documents\user\ 但它只是 /user

最佳答案

输入字符串开头的斜杠 (/) 显然会干扰 Path.Combine()。试试这个:

Path.Exists("user", 2);
MessageBox.Show(Path.RecentPath);

输出:C:\Users\Hossam\Documents\user

关于c# - 为什么函数输出错误的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25964081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com