2011-10-14 43 views
0

我正在編寫一個C#程序,它將檢查以確保它基於它的Web配置文件應該具有活動的所有連接都處於活動狀態,如果沒有,直到嘗試重新啓動連接,並告訴主機是否失敗或通過這樣做。將通過web.config文件的C#程序

我對web.config文件知之甚少,我知道它是XML,我認爲我想看到的點是活動的終點。

目前我能夠讀取該文件,但不能只是得到測試後的「端點=」

的想法/計劃的目標是讓我能夠重新啓動連接形成我的網頁應用程序到我的數據庫,如果由於某種原因它下降,並讓我知道,而我運行這個程序時,連接已關閉。

的Program.cs

using System; 
using System.IO; 
using System.Linq; 
using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Forms; 
namespace webconfig 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1());  
     }  
    } 
} 

表1

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace webconfig 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void richTextBox1_TextChanged(object sender, EventArgs e) 
     { 
      RTBconsole.Text = "" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year + "\r\n\r\n"; 

      // create reader & open file 
      string strFilename = "C:\\Sites\\EasyeServeIDSrv\\Web.config"; 
      FileStream fsVideos = new FileStream(strFilename,FileMode.Open,FileAccess.Read); 
      System.Xml.XmlTextReader rdrXml = new System.Xml.XmlTextReader(fsVideos); 

      StreamReader tr = new StreamReader("C:\\Sites\\EasyeServeIDSrv\\Web.config"); 

      do 
      { 
       String current = tr.ReadLine(); 
       // read a line of text 
       if (current.Contains("endpoint") == false || current.Contains("</endpoint>") == false) 
       { 
       RTBconsole.AppendText("  "+ current.ToString()); 
      }else{ 

      } 
      }while(!tr.EndOfStream); 

      do 
      { 
       // Read an item and return true 
       // Continue reading as long as ... 
      } while (rdrXml.Read() == true); // ... as long as Read() returns true 
      // Once Read() returns false, STOP!!! 

      fsVideos.Close(); 
      Console.WriteLine(); 
      // close the stream 
      tr.Close(); 
     } 
    } 
} 
+2

那麼,你卡在哪裏?向我們展示代碼! –

+1

另外,向我們展示一下你的web.config的樣子,以及你想要處理的部分。 – Jacob

+0

我被困在很早的時候:(現在我的代碼只會將文件寫入表單1上的RTB。 –

回答

2

web.config的每個部分對應於繼承自ConfigurationSection類的類。像這樣的東西應該讓你閱讀web.config的每個部分:

//get the configuration file 
Configuration config = WebConfigurationManager.OpenWebConfiguration("..."); //path to config 

//get smtpConfiguration section 
ConfigurationSection section = config.GetSection("smtpConfiguration"); 
1

使用命名空間System.configuration來操縱web.config總是更好。

thera中有一些類在讀取/寫入連接字符串和執行時的應用程序設置。