如何在C#的文本文件中更改未知号码?

时间:2017-08-31 17:33:36

标签: c# text-files discord

我正试图建立一个警告系统"对于我的Discord Server机器人,在使用该命令时,它会从文本文件中提取信息然后进行更新。我可以在某种程度上想象我想要一步一步地发生什么,但我很困惑如何将从文件中读取的内容作为一个例子:

鲍勃#5368 3
mark#8459 6
cindy#1254 2

我只想更改名称背后的数字,我不太确定如何找到我正在寻找的用户,然后提取数字并将其设置为' totalWarn',将其添加(totalWarn = + warnNum),然后使用新号码更新该用户。

注意:' warningFile'是已在此代码块之外定义的文件路径。

public async Task warn(IGuildUser user, int warnNum, [Remainder] string reason)
    {
        int totalWarn = 0;
        if (user == null)
            await ReplyAsync("Please include a name");
        if (warnNum <= 0)
            await ReplyAsync("The warning level must increase by 1 or more.");
        if (reason == null)
            await ReplyAsync("Please include a reason.");

        string nickName = user.Username;

        //Check for whether text file exists or not.
        if (!File.Exists(warningFile))
        {
            //Create the File
            FileStream fs = File.Create(warningFile);
        }

        //Reads all the text in the file and puts it into an array.
        string[] arr = File.ReadAllLines(warningFile);
        for (int i = 0; i < arr.Length; i++)
        {
            string line = arr[i];
        }



        totalWarn = +warnNum;
        await Context.Channel.SendMessageAsync($"{user.Username}'s warning level has increased by {warnNum} for {reason}.\n"
            + $"Your warning level is now {totalWarn}. Please take this time to review the server rules.");
    }

我没有找人为我完成它,只是帮助我自己朝着正确的方向努力,因为我完全失去了,而且有关更改文字的信息并不是很有帮助

2 个答案:

答案 0 :(得分:3)

以下是如何使用regex解析文件中的一行:

void Main()
{
    string line = "bob#5368 3";
    GuildUser user = new GuildUser( line );
    user.Warnings++;
    user.ToString().Dump();
}

public class GuildUser
{
    public string Name { get; set;}
    public int Id { get; set;}
    public int Warnings { get; set;}
    public GuildUser( string line )
    {
        Match match = Regex.Match( line, @"(.+)?#(\d+) (\d+)" );
        if ( !match.Success ) throw new Exception( "Couldn't parse line: " + line );
        Name = match.Groups[1].Value;
        Id = int.Parse( match.Groups[2].Value );
        Warnings = int.Parse( match.Groups[3].Value );

    }
    public override string ToString()
    {
        return $"{Name}#{Id} {Warnings}";
    }
}

您可以使用File.ReadAllLines,File.WriteAllLines。

我可能也会使用一些linq。

我更新了添加“ToString”方法来重写该行。 然后你可以添加警告并获取调用ToString()的新字符串。

答案 1 :(得分:0)

另一种方式:

    public static void stuff()
    {       
        string name = "bob";
        string firstNumber = "";
        string secondNumber = "";
        int lineIndex = 0;

        List<string> lines = new List<string>(File.ReadAllLines(@"C:\Text.txt"));

        foreach(string line in lines)
        {
            if(line.Contains(name))
            {
                lineIndex = lines.IndexOf(line);
                string[] parts = line.Split('#');
                name = parts[0];
                firstNumber = parts[1].Split(' ')[0];   
                secondNumber = parts[1].Split(' ')[1];

                firstNumber = (Convert.ToInt32(firstNumber) + 1).ToString();

                /*
                 * instert code here for changing the number you want to change
                */

                lines[lineIndex] = name + "#" + firstNumber + " " + secondNumber;
                File.WriteAllLines(@"C:\Text.txt",lines.ToArray());
                break;
            }
        }                   
    }