从一个公共部分获取变量到另一个

时间:2013-10-24 21:38:51

标签: c#

这里可能是一个非常简单的菜鸟问题。基本上我有一个txt文件,它有许多控制我写的服务的变量。目前在每个需要变量的空白中我都打开文件,读取文件,挑选行然后使用变量。

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

我编辑了我的问题并添加了一些代码以尝试更好地解释。 所以下面我有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");
    }

1 个答案:

答案 0 :(得分:0)

恕我直言我会建议从文件的设置静态类: 这样,只有在第一次访问类时才会读取文件。

希望这样的事情适合你:

更新: 填写读取文件逻辑并添加属性调用

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;