[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