【问题标题】:using variables stored in a file to trigger events使用存储在文件中的变量来触发事件
【发布时间】:2012-04-13 06:24:03
【问题描述】:

我对 C# 非常陌生,对文件的写入和读取非常陌生。无论如何,我正在制作一个事件序列,当计时器达到一个数字时将播放事件,同样在该文件中,计时器也将根据存储在文件中的数字调整其速度。我该怎么做呢,这是解码时该文件的文本。

 [EventSequence]
{
    DisplayName "Default"
    OFFset = 0
    Resolution = 192

}
 [SyncSequence]
{
0 = B 180000
}
 [EventsNotes]
{
0 = E "section Intro"
15168 = E "Note1"
21120 = E "Note2"
26880 = E "Note3"
38976 = E "Note4"
44928 = E "Note5"
}
 [Events]
{
192 = N 0 0
240 = N 0 0
288 = N 0 0
336 = N 0 0
384 = N 4 0
432 = N 0 0
480 = N 0 0
528 = N 0 0
576 = N 3 0
624 = N 0 0
672 = N 0 0
720 = N 0 0
768 = N 4 0
816 = N 0 0
864 = N 0 0
912 = N 0 0
960 = N 2 0
1008 = N 0 0
1056 = N 0 0
1104 = N 0 0
1152 = N 1 0
 }

计时器会很快,但取决于 Sync 下的值。大值是触发事件的时间,N 0 0 等是事件。 “事件说明”的类似设置。其余的只是基本信息。高级中的任何帮助或建议表示赞赏。

【问题讨论】:

  • 具体哪一部分有问题?
  • 你还没有真正问过问题。
  • 有效地找到标记部分之间的行例如,如果我想使用事件部分中的所有内容,我不想要其他部分的任何内容,并且我需要它可用于许多文件尺寸。问题是“我该怎么做。”
  • 请不要在标题前加上“C#”之类的前缀。这就是标签的用途。

标签: c# windows file


【解决方案1】:

我为你的情况做了这个功能:

public List<string> GetFileKeyValues(string fileName, string key)
        {
            List<string> res = new List<string>();
            try
            {
                if (!string.IsNullOrEmpty(key))
                {
                    using (System.IO.StreamReader tr = new System.IO.StreamReader(fileName))
                    {
                        bool keyFound = false;
                        while (!tr.EndOfStream)
                        {
                            string s = tr.ReadLine().ToLower();
                            if (s.Contains(key.ToLower())) keyFound = true;
                            else
                            {
                                if (keyFound)
                                {
                                    if (!s.Contains("{") && !s.Contains("}")) res.Add(s);
                                    if (s.Contains("}")) break;
                                }
                            }
                        }
                        tr.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return res;
        }

用法假设我们想要获取[SyncSequence] 值,所以:

List<string> res = GetFileKeyValues(@"C:\t.txt", "[SyncSequence]");
if(res != null && res.Count > 0) 
{
  //Do Something with res[0], it will return 0 = B 180000
  //So you split it by "=" to get B 180000 or any thing you want... 
} 

【讨论】:

  • 谢谢,这正是我想要的。在使用这些值时,我可以处理其余的事情。
猜你喜欢
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 2018-04-13
  • 2020-11-11
  • 2018-02-24
  • 2012-07-23
  • 2014-10-04
相关资源
最近更新 更多