如何在Main方法中返回带有字符串参数的bool方法

waq*_*qas 0 c#

我用字符串参数创建了一个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;
}
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*ter 6

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

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;
}
Run Code Online (Sandbox Code Playgroud)