c# - 更新目录中的 XML 文件
问题描述
我仍然是 C# 的新手,但我创建了一个实用程序,如果出于某种原因他们的 IP 地址要更改或他们想要更改它,存储的 IP 地址将更新为当前 IP 地址。
我的代码应该过滤目录中的所有 xml 配置文件并使用新的 IP 地址对其进行更新。然而,它们是某些文件夹中的一些 xml 配置文件,我不想像 net.tcp://localhost 那样更改它们。
<endpoint name="SessionLocal" address="net.tcp://localhost:7732/AuthenticationServices/Secure" binding="netTcpBinding" contract="Stuff.Services.ServiceContracts.IAuthenticationService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint name="DataLocal" address="net.tcp://localhost:7732/DataServices/Secure" binding="netTcpBinding" contract="Stuff.Services.ServiceContracts.IDataService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
接下来的几个文件夹包含我想要更改的 xml 文件,它们具有实际的 IP 地址,例如
<endpoint name="SubscriptionLocal" address="net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure" binding="netTcpBinding" contract="Stuff.Services.ServiceContracts.ISubscriptionService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="PublishLocal" address="net.tcp://10.249.30.4:7732/EventPublishServices/Secure" binding="netTcpBinding" contract="Stuff.Services.ServiceContracts.IPublishService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
1) 我将如何添加代码,以便用户可以选择手动输入 IP 地址,该 IP 地址将更新 xml 配置文件中的 Ipaddress。(如果 xml 配置文件中的端点地址没有 localhost 地址)
2)我将如何在 else 语句中编写代码以继续在目录中进行枚举?
static void Main(string[] args)
{
var dir = Directory.EnumerateDirectories(@"C:\Program Files (x86)\Stuff\Noodles");
foreach (var folder in dir)
{
var path = Directory.EnumerateFiles(folder, "*.config", SearchOption.AllDirectories);
string newIPAddress = "10.246.31.4";
foreach (var xmlfile in path)
{
var doc = XDocument.Load(xmlfile);
var endpointsToUpdate = doc
.Descendants("endpoint")
.Where(x => new Uri((string)x.Attribute("address")).Host != "localhost")
.ToArray();
// skip if there is nothing to update
//if (!endpointsToUpdate.Any()) return;
if (endpointsToUpdate.Any());
{
foreach (var endpoint in endpointsToUpdate)
{
string address = (string)endpoint.Attribute("address");
string pattern = "//\\d[^:]+";
address = Regex.Replace(address, pattern, "//" + newIPAddress);
endpoint.Attribute("address").SetValue(address);
}
else
{
}
doc.Save(xmlfile);
}
}
}
}
}
}
解决方案
只需将模式的第一个字符更改为 \d (数字)即可解决问题:
string newIP = "10.36.31.4";
string addr1 = "net.tcp://localhost:7732/AuthenticationServices/Secure";
string addr2 = "net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure";
string pattern = "//\\d[^:]+";
string newaddr1 = Regex.Replace(addr1, pattern, "//" + newIP);
string newaddr2 = Regex.Replace(addr2, pattern, "//" + newIP);
推荐阅读
- c++ - How to run tests and debug Google Test project in VS Code?
- python - 使用 Python 从 SAP Hana 加载数据时出现语法错误
- python - 使用条件语句对输出值进行分类
- mariadb - 物料清单和 CTE (MariaDB)
- pytorch - pytorch 后向误差,由就地操作修改的梯度计算变量之一
- java - 如何使用 Completable Future 进行外部 API 调用?
- javascript - 如何从 reader.result 获取主要的 base64 值?
- android - 如何将复选框数据传递给另一个活动
- linux - Nginx 反向代理做意外的负载均衡
- html - React Multi Step Forms - HMTL 警告不起作用