[英]C# program that will go through a web.config file
我正在編寫一個C#程序,該程序將檢查以確保基於其Web配置文件應具有的所有連接均處於活動狀態,如果沒有,請嘗試重新啟動連接並告知主機是否失敗或通過該連接為止。
我對web.config文件了解得很少,我知道它是XML,我認為要查看的要點是端點。
目前,我可以讀取文件,但不能在“ endpoint =“
該程序的目的/目標是讓我能夠重新啟動從Web應用程序到數據庫的連接(如果由於某種原因而斷開連接),並讓我知道我在運行此程序時連接已斷開。
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();
}
}
}
web.config的每個部分都對應於一個從ConfigurationSection
類繼承的類。 這樣的事情應該可以讓您閱讀web.config的每個部分:
//get the configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration("..."); //path to config
//get smtpConfiguration section
ConfigurationSection section = config.GetSection("smtpConfiguration");
您應該看一下使用LINQ to XML查詢web.config
中感興趣的元素。 這是一個使用LINQ to XML寫web.config
的人的示例 。
最好使用名稱空間System.configuration來操縱web.config。
Thera中有一些類在執行時讀取/寫入連接字符串和應用程序設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.