有一个文本文件的内容如下:
ClientEntityRemark=HuNan Power Trap Receive Program
#MsgSeparator
MsgSeparator=~
#Client Entity Name
ClientEntityName=ADP_SNMPTRAP_192.168.6.39
#Sms Server Entity Name
SmsServerEntityName=MGR_SmsServer
#Alarm Manager Entity Name
AlarmManagerEntityName=MGR_ALARM
#Software Monitor Entity Name
SWMonitorEntityName=MGR_SysSWMonitor
要把ClientEntityName=ADP_SNMPTRAP_192.168.6.39这句该成ClientEntityName=ADP_SNMPTRAP_本机ip地址
1 private void main()
2 {
3 GetIP();
4 string filename=@"C:\env.properties";
5 string strsearch = "ClientEntityName";
6 int charcount = 30;
7 modifyfile(filename,strsearch,charcount);
8 }
private void GetIP()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach(ManagementObject nic in nics)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(Convert.ToBoolean(nic["ipEnabled"]) == true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Console.WriteLine((nic["IPAddress"] as String[])[0]);
this.ip =(nic["IPAddress"] as String[])[0];
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
private void modifyfile(string filename ,string strsearch,int charcount)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
StreamReader sr = new StreamReader(filename,System.Text.Encoding.Default);
string strSouce = sr.ReadToEnd();
sr.Close();
string str = this.Getstring(strSouce,strsearch,charcount);
StreamWriter sw = new StreamWriter(filename);
sw.Write(str);
sw.Flush();
sw.Close();
}
private string Getstring(string strSouce,string strsearch,int charcount)//ip
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int strofindex =strSouce.IndexOf(strsearch,0);
string strlastPart = strSouce.Substring(strofindex+charcount);
int ipindex = strlastPart.IndexOf("\r\n",0);
string strip = strlastPart.Substring(0,ipindex);
//ip的第四部分不一样
switch (strsearch)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ case "ClientEntityName":
strip = this.ip.ToString();
break;
}