C# 获取文件路径的最佳方法是什么

C# 获取文件路径的最佳方法是什么,c#,C#,我有3个不同的目录,其中的文件可供进一步处理 可能是某个目录不存在,因此我放置了try/catch 只要任何目录中有一个文件可用,我就返回路径 下面是代码,问题是,获得上述功能的文件路径的最佳方法是什么 private static string GetRealPath() { const string filePath1 = @"C:\temp1\"; const string filePath2 = @"C:\temp2\"; cons

我有3个不同的目录,其中的文件可供进一步处理


  • 可能是某个目录不存在,因此我放置了
    try/catch
  • 只要任何目录中有一个文件可用,我就返回路径
  • 下面是代码,问题是,获得上述功能的文件路径的最佳方法是什么

    private static string GetRealPath()
        {
            const string filePath1 = @"C:\temp1\";
            const string filePath2 = @"C:\temp2\";
            const string filePath3 = @"C:\temp\";
    
            //looks for file in  @"C:\temp1\ 
            try
            {
                if (Directory.EnumerateFileSystemEntries(filePath1, "*.txt").ToList().Count > 0)
                {
                    return filePath1;
                }
            }
            catch (Exception e) { Console.WriteLine(e); }
    
            //looks for file in  @"C:\temp2\ 
            try
            {
                if (Directory.EnumerateFileSystemEntries(filePath2, "*.txt").ToList().Count > 0)
                {
                    return filePath2;
                }
            }
            catch (Exception e) { Console.WriteLine(e); }
    
            //looks for file in  @"C:\temp\ 
            try
            {
                if (Directory.EnumerateFileSystemEntries(filePath3, "*.txt").ToList().Count > 0)
                {
                    return filePath3;
                }
            }
            catch (Exception e) { Console.WriteLine(e); }
    
            return string.Empty;
        }
    

    您可以改用
    目录。Exists

    public static string GetFirstValidPath()
    {
        string[] paths = { @"C:\temp1\", @"C:\temp2\", @"C:\temp\"};
        return paths.FirstOrDefault(p=> Directory.Exists(p) 
           && Directory.EnumerateFileSystemEntries(p, "*.txt").Any()) ?? string.Empty;
    }
    

    您可以改用
    目录。Exists

    public static string GetFirstValidPath()
    {
        string[] paths = { @"C:\temp1\", @"C:\temp2\", @"C:\temp\"};
        return paths.FirstOrDefault(p=> Directory.Exists(p) 
           && Directory.EnumerateFileSystemEntries(p, "*.txt").Any()) ?? string.Empty;
    }
    

    “可能是某个目录不存在,因此我设置了try/catch”-我觉得您应该使用
    目录。Exists
    您到底想获取的路径是什么?temp?Get path to what?
    枚举文件系统(filePath3,*.txt”)。ToList().Count>0
    效率极低,尤其是在包含许多文件的目录上。您总是要检查每个文件,以确定是否有一个或多个文件。您应该使用
    EnumerateFileSystemEntries(filePath3,*.txt”)。Any()
    “可能是某个目录不存在,因此我放置了try/catch”-听起来您应该使用
    directory.Exists
    您到底想获取的路径是什么?temp?Get path to what?
    枚举文件系统(filePath3,*.txt”)。ToList().Count>0
    效率极低,尤其是在包含许多文件的目录上。您总是要检查每个文件,以确定是否有一个或多个文件。您应该使用
    枚举文件系统(filePath3,*.txt”).Any()
    。小问题:如果不匹配,您的答案将返回
    default(string)
    ,而不是
    string.Empty
    ,并且
    default(string)!=string.Empty
    @user584018:返回
    string.Empty
    。固定的。相反,您也可以在调用此方法的地方处理它。如果没有有效路径,则返回
    null
    似乎是更好的选择。小问题:如果不匹配,您的答案将返回
    default(string)
    ,而不是
    string。空的
    ,以及
    default(string)!=string.Empty
    @user584018:返回
    string.Empty
    。固定的。相反,您也可以在调用此方法的地方处理它。如果没有有效路径,则返回
    null
    似乎是更好的选择。