C # winform Tip 7: Read Process Information

Source: Internet
Author: User

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.  "  );}} 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.