2013-10-24 136 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

很好,謝謝。 !這會讓這一切變得更容易。 –

相關問題