c# - 如何在 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。
解决方案
您首先读取文件,然后检查它是否存在。当然你必须使用另一种方式:
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;
}
推荐阅读
- javascript - DOMContentLoaded 不会在 IOS Safari 上触发
- java - 有人可以解释这两个简单的循环吗?
- python - 比较python中的两个数组以查看它们是否共享任何值
- typescript - 如何使用 map 运算符将新字段添加到对象中,并将其添加到返回到 Observable 的数组中?
- ant - 如何通过命令行选项将命令行选项传递给 java 编译器
蚂蚁任务使用 ? - git - 来自 Windows 10 主机的 Centos 7 devcontainer 中的 GPG 签名提交
- jquery - 固定页眉出现在不同页面的不同位置
- node.js - 如何使用 Google Closure Compiler 浏览您的 Node.js 库
- python - 有没有办法在 Kivy 弹出窗口中调整内容的大小
- r - 如何在R中的foreach()中更改数据框的某些列的长度