【发布时间】:2020-04-10 05:01:10
【问题描述】:
我仍然是 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>
接下来的几个文件夹包含我想要更改的具有实际 IP 地址的 xml 文件,例如
<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 地址,以更新 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);
}
}
}
}
}
}
【问题讨论】:
-
我从以前的帖子中认出了我的代码。试试: XElement[] endpointsToUpdate = doc .Descendants("endpoint") .Where(x => (string)x.Descendants("dns").FirstOrDefault().Attribute("value") != "localhost") 。 ToArray();
-
没有任何改变。当我在“if (!endpointsToUpdate.Any()) return;”之后单步执行它时它跳过了 foreach 语句,所以它几乎就像是因为第一个文件夹在它的配置文件中有 localhost。代码读取它,因为没有什么要更新的,所以它只是结束程序
-
你所有的样本都有本地主机,所以它什么都不返回。将 localhost 更改为示例中的其他内容。
-
我不是要更改 dns 值,dns 值应该始终是本地主机。我正在尝试将端点地址更新为 newIPAdress,如果它具有像“net.tcp://10.249.30.4:7732”这样的实际 IP 地址,但如果它具有 net.tcp://localhost:7732/ 那么它应该是单独留下。我想我可能给你造成了误解,我很抱歉
-
我试图按照你上面黄色的代码,它正在测试!=“localhost”。所以我的代码给出了不包含本地主机的元素。