修改eado.ini文件C#
文件内容
[DEFAULT]
NICKNAME=Mu3Seagate
FIRSTNAME=wang
LASTNAME=123456
SINGILECONNECTION=1
[HP]
NICKNAME=HP
FIRSTNAME= 123456
LASTNAME= 52631
SINGILECONNECTION=1
[RMA189]
NICKNAME=RMA
FIRSTNAME=5891
LASTNAME=5555555
SINGILECONNECTION=1
public partial class Form1 : Form { List<FileModel> listFile =null; string strDbName = string.Empty; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { listFile = new List<FileModel>(); ReadDB(); textBox1.Text = "当前数据库是:" + strDbName; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button1_Click(object sender, EventArgs e) { try { if (strDbName.Equals("Mu3Seagate")) { RegODBC(strDbName, AppConfig.targetIP); updateFile(); } else throw new Exception("当前数据库不是Mu3Seagate,不能修改"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void updateFile() { listFile.Where(x => x.dns.ToUpper().Equals("DEFAULT")).ToList().FirstOrDefault().FIRSTNAME = "1111"; listFile.Where(x => x.dns.ToUpper().Equals("DEFAULT")).ToList().FirstOrDefault().LASTNAME = "2222"; listFile.Where(x => x.dns.Equals("Mu3Seagate")).ToList().FirstOrDefault().FIRSTNAME = "1111"; listFile.Where(x => x.dns.Equals("Mu3Seagate")).ToList().FirstOrDefault().LASTNAME = "2222"; File.Delete(@"C:\Windows\Eado.ini"); listFile.ForEach(x=> { List<string> temp = new List<string>(); temp.Add("[" + x.dns + "]"); temp.Add("NICKNAME=" + x.NICKNAME); temp.Add("FIRSTNAME=" + x.FIRSTNAME); temp.Add("LASTNAME=" + x.LASTNAME); temp.Add("SINGILECONNECTION=" + x.SINGILECONNECTION); File.AppendAllLines(@"C:\Windows\Eado.ini", temp,Encoding.UTF8); File.AppendAllLines(@"C:\Windows\Eado.ini",new List<string> { ""}, Encoding.UTF8); //File.AppendAllText(@"C:\Windows\Eado.ini", "\r\n"); }); } public void ReadDB() { List<string> list = new List<string>(); list = File.ReadAllLines(@"C:\Windows\Eado.ini").ToList().Where(x => !string.IsNullOrEmpty(x)).ToList(); List<string> listdns = new List<string>(); //0,5,10 List<string> listNICKNAME = new List<string>(); //1,6,11 List<string> listFIRSTNAME = new List<string>(); //2,7,12 List<string> listLASTNAME = new List<string>(); //3,8,13 List<string> listSINGILECONNECTION = new List<string>(); //4,9,14 for (int i = 0; i < list.Count; i++)//等差数列算法 { if (i % 5 == 0) listdns.Add(list[i].Replace("[","").Replace("]","").Trim()); else if (i % 5 == 1) listNICKNAME.Add(list[i].Replace("NICKNAME", "").Replace("=", "").Trim()); else if (i % 5 == 2) listFIRSTNAME.Add(list[i].Replace("FIRSTNAME", "").Replace("=", "").Trim()); else if (i % 5 == 3) listLASTNAME.Add(list[i].Replace("LASTNAME", "").Replace("=", "").Trim()); else if (i % 5 == 4) listSINGILECONNECTION.Add(list[i].Replace("SINGILECONNECTION", "").Replace("=", "").Trim()); } listFile.Clear(); for (int i = 0; i < listdns.Count; i++) { listFile.Add(new FileModel() { dns = listdns[i],NICKNAME =listNICKNAME[i],FIRSTNAME= listFIRSTNAME[i], LASTNAME=listLASTNAME[i], SINGILECONNECTION =listSINGILECONNECTION[i] }); } strDbName = listFile.Where(x => x.dns.ToUpper().Equals("DEFAULT")).ToList().FirstOrDefault().NICKNAME; } /// <summary> /// 注册ODBC数据源 /// </summary> /// <param name="DsnName">ODBC数据源名称,这里要与SQL Server数据库名保持一致</param> /// <param name="ServerName">SQL Server数据库服务器名</param> /// <returns>返回是否成功</returns> private void RegODBC(string DsnName, string ServerName) { string DBname = string.Empty; string strText = File.ReadAllText(@"C:\Windows\Eado.ini"); Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ODBC"). OpenSubKey("ODBC.INI", true).DeleteSubKey(DsnName.Trim()); //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI中创建一个子键和相应的值 Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software"). OpenSubKey("ODBC").OpenSubKey("ODBC.INI", true).CreateSubKey(DsnName.Trim()); regkey.SetValue("DataBase", "efoxsfcmu3seagate"); regkey.SetValue("Driver", @"C:\WINDOWS\System32\SQLSRV32.dll"); regkey.SetValue("Server", ServerName.Trim()); regkey.SetValue("LastUser", "essnlxddl"); regkey.SetValue("Trusted_Connection", "No");//如果是账密登录, //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI/ODBC Data Sources中增加一个字符串键值 regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ODBC") .OpenSubKey("ODBC.INI", true).OpenSubKey("ODBC Data Sources", true); regkey.SetValue(DsnName.Trim(), "SQL Server"); } }