Automatic Configuration Software
The main software is already running on the computer. Now I want to start the related gadgets. I want to configure the same software parameters, such as database connection configuration, the tool automatically reads the configuration information from the main software to the tool.
Requirements:
Change Point:
1: The main software is not running, and the gadgets can still run. You need to manually configure the gadgets.
Steps:
1: Read Process
2: Get the process location
3: Find the required configuration file
4: Read the configuration file information to the gadgets
5. Save configuration information for the gadgets.
Implementation:
Master Software Process Information:
Find the configuration file:
<? XML version = "1.0" encoding = "UTF-8" ?> < Configuration > < Appsettings > <! -- Connection string --> < Add Key = "Server" Value = "LUOMG-PC" /> < Add Key = "Uid" Value = "Sa" /> < Add Key = "PWD" Value = "123456" /> < Add Key = "DB" Value = "Heatingfs" /> </ Appsettings > </ Configuration >
Interface operation:
Read Source Code:
// Read process to get configuration Private Void Button#click ( Object Sender, eventargs e ){ Try { # Region Read process to get configuration Process [] myprocess = Process. getprocessesbyname ( " Hailin " ); Foreach (Process PS In Myprocess ){ String Filename =PS. mainmodule. filename; String Configfile = path. getdirectoryname (filename) + @" \ Hailin.exe " ; If (File. exists (configfile) {configuration _ innerconfig = Configurationmanager. openexeconfiguration (configfile ); If (_ Innerconfig. deleettings. settings. Count> 0 ) {Txt_server.text = _ Innerconfig. deleettings. settings [ " Server " ]. Value; txt_database.text = _ Innerconfig. deleettings. settings [ " DB " ]. Value; txt_sa.text = _ Innerconfig. deleettings. settings [ " UID " ]. Value; txt_pwd.text = _ Innerconfig. deleettings. settings [ " PWD " ]. Value; MessageBox. Show ( " Read successful! Click Save settings. " );}} Else {MessageBox. Show ( " The configuration file does not exist! " + Configfile );}} # Endregion } Catch {MessageBox. Show ( " Reading failed! Enter the configuration information manually. " );}}