[英]Updating XML Files inside a directory
我仍然是 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 地址,以更新 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);
}
}
}
}
}
}
如果您使用正則表達式只匹配不包含特定字符串(即 localhost)的字符串怎么辦? 因此,如果字符串不包含 localhost,請執行替換,否則跳過替換。 反之亦然,無論您選擇如何看待它。
^(?!. DontMatchThis)。 $
只需將模式的第一個字符更改為 \\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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.