0

这里可能是一个非常简单的菜鸟问题。基本上我有一个 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");
    }
4

1 回答 1

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;
于 2013-10-24T21:55:38.460 回答