编程自学网编程自学网编程自学网

从GitHub文本文件中读取特定值

问题描述:

我想从互联网上的文本文件中读取一个单词的特定分配。

在输出“内容”中,我获得了文本文件的完整内容。

但我只想从行中获得v7.7.3:version =“v7.7.3”。

如何使用streamreader按版本过滤?

这是LastVersion.txt文件:

[general]
version    = "v7.7.3"
messagenew = "Works with June 2018 Update!\n Plus new Smart Farm strategy\n New Siege Machines\n For more information, go to \n https://mybot.run \n Always free and open source."
messageold = "A new version of MyBot (v7.7.3) is available!\nPlease download the latest from:\nhttps://mybot.run"

更新:这是我目前的代码。

public string myBotNewVersionURL = "https://raw.githubusercontent.com/MyBotRun/MyBot/master/LastVersion.txt";public string myBotDownloadURL = null;public string userDownloadFolder = @"C:\Users\XXX\Download\";public string newMyBotVersion = null;public string currentMyBotVersion = null;public string currentMyBotFileName = null;public string currentMyBotPath = null;public void Btn_checkUpdate_Click(object sender, EventArgs e){
    OpenFileDialog openCurrentMyBot = new OpenFileDialog();
        openCurrentMyBot.Title = "Choose MyBot.run.exe";
        openCurrentMyBot.Filter = "Application file|*.exe";
        openCurrentMyBot.InitialDirectory = userDownloadFolder;        if (openCurrentMyBot.ShowDialog() == DialogResult.OK)
        {
            MyBot_set.SetValue("mybot_path", Path.GetDirectoryName(openCurrentMyBot.FileName));
            MyBot_set.SetValue("mybot_exe", Path.GetFullPath(openCurrentMyBot.FileName));            string latestMyBotPath = Path.GetFullPath(openCurrentMyBot.FileName);            var latestMyBotVersionInfo = FileVersionInfo.GetVersionInfo(latestMyBotPath);
            currentMyBotVersion = "v" + latestMyBotVersionInfo.FileVersion;

            MyBot_set.SetValue("mybot_version", currentMyBotVersion);
            WebClient myBotNewVersionClient = new WebClient();
            Stream stream = myBotNewVersionClient.OpenRead(myBotNewVersionURL);
            StreamReader reader = new StreamReader(stream);
            String content = reader.ReadToEnd();            var sb = new StringBuilder(content.Length);            foreach (char i in content)
            {                if (i == '\n')
                {
                    sb.Append(Environment.NewLine);
                }                else if (i != '\r' && i != '\t')
                    sb.Append(i);
            }
            content = sb.ToString();            var vals = content.Split(                                        new[] { Environment.NewLine },
                                        StringSplitOptions.None
                                    )
                        .SkipWhile(line => !line.StartsWith("[general]"))
                        .Skip(1)
                        .Take(1)
                        .Select(line => new
                        {
                            Key = line.Substring(0, line.IndexOf('=')),
                            Value = line.Substring(line.IndexOf('=') + 1).Replace("\"", "").Replace(" ", "")
                        });
            newMyBotVersion = vals.FirstOrDefault().Value;
}


解决方法:

从当地阅读

  var vals = File.ReadLines("..\\..\\test.ini")
                    .SkipWhile(line => !line.StartsWith("[general]"))
                    .Skip(1)
                    .Take(1)
                    .Select(line => new
                     {
                         Key = line.Substring(0, line.IndexOf('=')),
                         Value = line.Substring(line.IndexOf('=') + 1)
                     });

    Console.WriteLine("Key : " + vals.FirstOrDefault().Key +                      " Value : " + vals.FirstOrDefault().Value);

更新

对于从Git读取,File.ReadLines不适用于URL。

string myBotNewVersionURL = "https://raw.githubusercontent.com/MyBotRun/MyBot/master/LastVersion.txt";

            WebClient myBotNewVersionClient = new WebClient();
            Stream stream = myBotNewVersionClient.OpenRead(myBotNewVersionURL);
            StreamReader reader = new StreamReader(stream);            String content = reader.ReadToEnd();            var sb = new StringBuilder(content.Length);
            foreach (char i in content)
            {                if (i == '\n')
                {
                    sb.Append(Environment.NewLine);
                }                else if (i != '\r' && i != '\t')
                    sb.Append(i);
            }

            content = sb.ToString();            var vals = content.Split(                                        new[] { Environment.NewLine },
                                        StringSplitOptions.None
                                    )
                        .SkipWhile(line => !line.StartsWith("[general]"))
                        .Skip(1)
                        .Take(1)
                        .Select(line => new
                        {
                            Key = line.Substring(0, line.IndexOf('=')),
                            Value = line.Substring(line.IndexOf('=') + 1)
                        });


            Console.WriteLine("Key : " + vals.FirstOrDefault().Key + " Value : " + vals.FirstOrDefault().Value);


未经允许不得转载:编程自学网 » 从GitHub文本文件中读取特定值