[C # Windows Service] "2" INI configuration file,

Source: Internet
Author: User

[C # Windows Service] "2" INI configuration file,

Directory:

1.[C # Windows Service] Getting started with "1"

2.[C # Windows Service] INI configuration file

 

I. tools:

VS2015 + NET Framework4.5.

 

Ii. Operations:

1. Create the INIHelp help class

 

2. Rich help operations

 

3. windows instance call

 

 

Iii. Code:

1. INI help class:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Runtime.InteropServices;namespace ClassLibrary1{    public class INIHelp    {        public static string iniFileName = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.DirectorySeparatorChar + "config.ini";        [DllImport("kernel32")]        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);        [DllImport("kernel32")]        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);        public static string SaveConfig()        {            try            {                WriteIni("sqlconfig", "testsql", "select *  from Dog");            }            catch (Exception ex)            {                LogHelp.WriteLog(ex.ToString());            }            return "0";        }        public static void WriteIni(string Section, string Key, string strValue)        {            WritePrivateProfileString(Section, Key, strValue, iniFileName);        }        public static string ReadIni(string Section, string Key, string Default)        {            StringBuilder temp = new StringBuilder(1024);            int rec = GetPrivateProfileString(Section, Key, Default, temp, 1024, iniFileName);            return temp.ToString();        }    }}

  

2. Call code in windows Service:

Using ClassLibrary1; using System. collections. generic; using System. componentModel; using System. data; using System. diagnostics; using System. linq; using System. serviceProcess; using System. text; using System. threading; using System. threading. tasks; namespace WindowsServiceTest {public partial class Service1: ServiceBase {public Service1 () {InitializeComponent ();} protected override void OnStart (string [] args) {Thread thread = new Thread (delegate () {try {// for (int I = 0; I <1000; I ++) // {LogHelp. writeLog ************ *****"); INIHelp. writeIni ("sqlconfig", "testsql", "select * from Dog"); string str = INIHelp. readIni ("sqlconfig", "testsql", ""); LogHelp. writeLog (str); LogHelp. writeLog *********** ******"); //} catch (Exception ex) {LogHelp. writeLog ("service startup failed" + ex) ;}}); thread. name = "thread Test 1"; thread. isBackground = true; thread. start () ;}protected override void OnStop (){}}}

  

 

 

Iv. Summary:

 

 

 

Record every day, code every line of code

 

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.