C# 用c中的自定义值检查行#
伙计们,我用这个简单的代码来读取mp-config.cfg文件,但只是读取。 如何检查此行:C# 用c中的自定义值检查行#,c#,line,C#,Line,伙计们,我用这个简单的代码来读取mp-config.cfg文件,但只是读取。 如何检查此行: r_full "value" 如果此行的值为“0”,则返回此错误: "Sorry but have to use full Screen mod." 然后强制将值写入“1” 我的代码: using System; using System.IO; namespace checkFull { class TextFileReader { static void Main(st
r_full "value"
如果此行的值为“0”,则返回此错误:
"Sorry but have to use full Screen mod."
然后强制将值写入“1”
我的代码:
using System;
using System.IO;
namespace checkFull
{
class TextFileReader
{
static void Main(string[] args)
{
Textreader tr = new StreamReader(@"player\mp-config.cfg");
how do that?
// close the stream
tr.Close();
}
}
}
所以你假设我们都知道什么是
mp config.cfg
。但如果您发布示例输入和预期输出,我们可能会有所帮助。文件mp-config.cfg有多行(文本格式),其中一行是r_full“1/0”,我需要检查此值是1还是0,但mp-config.cfg是多行文件,我只需要检查行“r_full”,并更新此行的值(如果是“0”,则更改为“1”)secondWord
实际上是文件的第二个单词。不管有多少行。
string[] parts = File.ReadLines(@"player\mp-config.cfg")
.Select(line => line.Split())
.Where(x => x.Length > 1)
.FirstOrDefault(x => x[0] == "r_full");
string result="";
if (parts != null)
{
result = parts[1];
}
class TextFileReader
{
static void Main(string[] args)
{
string path = @"player\mp-config.cfg";
string readText = File.ReadAllText(path);
string secondWord = readText.Split()[1];
int value = Int32.Parse(secondWord);
if (value == 0)
{
Console.WriteLine("Sorry but have to use full Screen mod.");
using (StreamWriter outfile = new StreamWriter(path))
{
outfile.Write("r_val 1");
}
}
}
}