2013-10-24 112 views
0

这里可能是一个非常简单的noob问题。基本上我有一个txt文件,它有许多控制我写的服务的变量。目前在每个需要一个变量的void中,我打开文件,读取文件,选择行并使用变量。从一个公共区域获取变量到另一个

我的问题是,是否有一种方法来全局分配变量。这样我所有的空洞都可以使用它们。这样,我只需要在读取所有内容后打开文件,将所有内容分配给一个变量。而不是我现在正在做的那种方式,我多次打开文件,在多数情况下寻找相同的变量。

我编辑了我的问题,并添加了一些代码以尝试更好地解释。 所以下面我有2个空隙。他们都打开相同的文件,但在文件中查找不同的行。我想知道是否可以创建一个读取整个文件的“全局”变量列表,然后我可以调用我需要的变量,而不必每次打开文件都需要信息。

 public void day_timer() 
    { 
     string temptimer = ""; 
     string timeloop = ""; 
     using (var streamReader = System.IO.File.OpenText(@"C:\somefile")) 
     { 
      var lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
      foreach (var line in lines) 
      { 
       if (line.Contains("Day_Time:")) 
       { 
        temptimer = line; 
        continue; 
       } 
      } 
     } 
     timeloop = temptimer.Remove(0, 9); 
     int inter = Convert.ToInt32(timeloop); 
     System.Timers.Timer timer1 = new System.Timers.Timer(); 
     InitializeComponent(); 
     timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed); 
     timer1.Interval = inter * 1000 * 60; 
     timer1.Enabled = true; 
     timer1.Start(); 
     error_handling("backup started " + DateTime.Now + ". Incremental Backup set to every " + timeloop + " Minutes", "Incbackuplog.txt"); 
    } 

    //Incrememtal Backup Timer 
    public void inc_timer() 
    { 
     string temptimer = ""; 
     string timeloop = ""; 
     using (var streamReader = System.IO.File.OpenText(@"C:\somefile")) 
     { 
      var lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
      foreach (var line in lines) 
      { 
       if (line.Contains("Inc_interval:")) 
       { 
        temptimer = line; 
        continue; 
       }     
      } 
     } 
     timeloop = temptimer.Remove(0, 13); 
     int inter = Convert.ToInt32(timeloop); 
     System.Timers.Timer timer1 = new System.Timers.Timer(); 
     InitializeComponent(); 
     timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed); 
     timer1.Interval = inter * 1000 * 60; 
     timer1.Enabled = true; 
     timer1.Start(); 
     error_handling(" Backup Started "+DateTime.Now+". Incremental Backup set to every "+timeloop+" Minutes", "Incbackuplog.txt"); 
    } 
+2

有很多方法可以做到这一点,很难推荐没有一些代码或更多信息的路径。 –

+0

是的。你有什么特别的问题?发布您的当前代码可能会有帮助,因为它可能会突出显示阻止您的内容。但是你似乎明白你的描述需要做什么,那么问题是什么? –

+1

如果你正在寻找建议,那么如果我要实现这样的功能,我会创建一个类,该属性与来自文本文件的变量相匹配,并且有一些像'bool isFileRead'这样的标志。然后,当我想使用某个变量时,我首先检查标志 - 如果'isFileRead'等于false,我将读取文本文件并赋值,否则它已经被读取并使用类属性。 – Leron

回答

0

恕我直言,我会建议一个静态类从文件中的设置: 这样,当类是在第一次访问该文件是只读的。

希望这样的事情是适合你:

UPDATE: 填写在读文件的逻辑和添加的属性调用

public static class Configuration 
{ 
    // public set so you can change the configfile location if necessary 
    public static string ConfigurationFile { get; set; } 

    // variables from configuration file, private set so they cannot be changed except by changing the configuration and reloading 
    public static string Inc_interval { get; private set; } 
    public static string Day_Time { get; private set; } 

    /// <summary> 
    /// Static constructor - will be called the first time this class is accessed 
    /// </summary> 
    static Configuration() 
    { 
     ConfigurationFile = @"C:\somefile"; 
     ReadConfigFile(); 
    } 

    /// <summary> 
    /// Calling this method will reload the configuration file 
    /// </summary> 
    public static void Reload() 
    { 
     ReadConfigFile(); 
    } 

    /// <summary> 
    /// Logic for reading the configuration file 
    /// </summary> 
    private static void ReadConfigFile() 
    { 
     // ToDo: Read file here and fill properties 
     using (StreamReader rd = new StreamReader(ConfigurationFile)) 
     { 
      while (!rd.EndOfStream) 
      { 
       string line = rd.ReadLine(); 
       if (line.StartsWith("Day_Time:")) 
        Day_Time = line.Remove(0, 9); 
       else if (line.StartsWith("Inc_interval:")) 
        interval = line.Remove(0, 13); 
      } 
     } 

    } 
} 

因为它是一个静态类,你可以访问它的属性所有的类,方法,无论如何:

timeloop = Configuration.Day_Time; 
+0

有趣,这是有点整齐。我一直试图通过虚拟学院来了解更多关于课程等的内容,但上面的例子很好。那么我可以从那个地方的任何地方调用变量对吗? –

+0

是的,因为属性是静态的,所以你不需要该类的对象。当你第一次对这个类做些什么时,静态构造函数被调用并读取文件。另外,这个类被声明为public,所以你可以从你的项目的所有其他类/方法中调用它。 – Annihil

+0

很好,谢谢。 !这会让这一切变得更容易。 –

相关问题