【发布时间】:2020-04-07 22:45:40
【问题描述】:
我有一个软件,它在安装时会询问我服务器的 IP 地址,并将该地址存储到不同文件夹中的多个配置文件中。我拥有的这段代码在一个配置文件中更新了配置文件的端点地址。
我将如何修改代码以查看软件的安装位置并更新安装软件的不同文件夹内的所有配置文件中的所有 IP 地址。
所有带有 IP 地址的配置文件看起来有点像我的示例。
配置文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace ConfigTool
{
class Class1
{
// foreach (Environment.SpecialFolder folder_type in Enum.GetValues(typeof(Environment.SpecialFolder)))
// {
// DescribeFolder(folder_type);
// }
//txtFolders.Select(0, 0);
//const string FILENAME = Environment.GetFolderPath(Environment.SpecialFolder.);
const string FILENAME = @"C:\Program Files (x86)\******\***\***\*****\*****.****.****.exe.config";
public static IPAddress GetIPAddress(string hostName)
{
Ping ping = new Ping();
var replay = ping.Send(hostName);
if (replay.Status == IPStatus.Success)
{
return replay.Address;
}
return null;
}
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> endpoints = doc.Descendants("endpoint").ToList();
foreach (var endpoint in endpoints )
{
string address = (string)endpoint.Attribute("address");
string newIp = "10.249.30.4";
string pattern = "//[^:]+";
address = Regex.Replace(address, pattern, "//" + newIp);
endpoint.Attribute("address").SetValue(address);
}
doc.Save(
XMLExample
<endpoint name="***Local" address="net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.ISubscriptionService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="***Local" address="net.tcp://10.243.32.4:7732/EventPublishServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.IPublishService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
<identity>
<dns value="*******" />
</identity>
</endpoint>
<endpoint name="****" address="net.tcp://10.243.32.4:7732/AuthenticationServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.IAuthenticationService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
【问题讨论】:
-
那么您的具体问题是什么?你在这方面有什么困难?
-
"如何修改代码以查看软件的安装位置,并更新安装软件的不同文件夹中所有配置文件中的所有 IP 地址。"
-
这个问题太宽泛了。您的问题是关于查找文件还是更新 XML?同样,您的具体问题是什么?将您想做的事情分解为任务,然后开始执行其中一项任务。第一个可能是定位文件。让它工作。然后开始研究如何更新它们。当您为第一个任务编写代码并遇到困难时,您可以就该特定代码寻求帮助。
-
我注释掉的代码是为任务写的代码。我试图创建一个 Environment.SpecialFolder 以查看软件的安装位置并通过该目录中的 xml 配置运行,但我不熟悉 Environment.Special 方法。我希望有人能指出我正确的方向
-
您注释掉的代码不是问题。如果您的问题与使用
Environment.SpecialFolder功能有关,请对其进行编辑以询问该主题。如果您在此处需要帮助,您需要在帖子中提出具体问题。我已经要求您这样做了两次,但您仍然没有在您的帖子中发送edit 以澄清您所要求的具体是什么。 (顺便说一句,你不能只是不断地输入通配符*并希望它做某事,因为它不会。文件路径通配符必须是有效的。)
标签: c# xml visual-studio xml-parsing