首页 > 解决方案 > 如何在 Main 方法中返回带有字符串参数的 bool 方法

问题描述

我创建了一个带有字符串参数的 bool 方法。虽然值 true 它有效,但 false 它给出错误。在 main 方法中调用 bool 方法时,它不接受来自 bool 方法的相同字符串参数。

public static bool init_access(string file_path)
{

    int counter = 0;
    file_path = @"C:\Users\waqas\Desktop\TextFile.txt";
    List<string> lines = File.ReadAllLines(file_path).ToList();
    foreach (string line in lines)

    {

        counter++;
        Console.WriteLine(counter + " " + line);
    }
    if (File.Exists(file_path))
    {

        return (true);

    }

    return false;
}

如果文件确实存在,则应返回 true,否则应返回 false。

标签: c#

解决方案


您首先读取文件,然后检查它是否存在。当然你必须使用另一种方式:

public static bool init_access(string file_path)
{
    if (!File.Exists(file_path))
    {
        return false;
    }

    int counter = 0;
    string[] lines = File.ReadAllLines(file_path);
    foreach (string line in lines)    
    {   
        counter++;
        Console.WriteLine(counter + " " + line);
    }

    return true;
}

推荐阅读