开发者

C# program that will go through a web.config file

I am writing a C# program that will check to ensure that all the connections it should have active based on it's web config file are active and if not it till try to restart the connection and tell the host if it fails or passes at doing so.

I have very little understanding of the web.config file I know it's XML and I think the point's I want to see are active are end points.

currently I am able to read the file but not able to just get the test after the "endpoint="

The idea/goal of the program is to let me be able to restart a connection form my web app to my database if for some reason it dropped and to let me know that I while i run this program that the connection was down.

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());     
        }    
    }
}

Form 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);

    开发者_StackOverflow社区        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();
        }
    }
}


Each section of the web.config corresponds to a class inheriting from the ConfigurationSection class. Something like this should allow you to read each section of the web.config:

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

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


You should look at using LINQ to XML to query your web.config for elements of interest. Here is an example of a person writing to the web.config using LINQ to XML.


It's always better to manipulate the web.config using the namespace System.configuration.

There are some classes in thera that reads/writes the connection strings and app settings on execution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜