一、工具:

VS2015+NET Framework4.5。

二、操作:

1、创建INIHelp帮助类

2、丰富帮助类操作

3、windows实例调用

三、代码:

1、INI帮助类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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、windows服务中调用代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using ClassLibrary1;
using System;
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("服务启动失败" + ex); ;
                }
            }
                );
            thread.Name = "线程测试1";
            thread.IsBackground = true;
            thread.Start();
 
        }
 
        protected override void OnStop()
        {
        }
    }
}

  

四、总结:

记录每一天的点滴,码好每一行的代码

【C#Windows 服务】 《二》INI配置文件的更多相关文章

  1. Windows服务二:测试新建的服务、调试Windows服务

    一.测试Windows服务 为了使Windows服务程序能够正常运行,我们需要像创建一般应用程序那样为它创建一个程序的入口点.像其他应用程序一样,Windows服务也是在Program.cs的Main ...

  2. WCF寄宿windows服务二

    如果有很多WCF服务需要寄宿,需要额外做一些工作:总体思路是:先把这些WCF服务的程序集打包,然后利用反射加载各个WCF服务的程序集,按顺序一个一个寄宿.先来看看我们需要寄宿的WCF服务: 实现步骤: ...

  3. C#创建windows服务(二:创建和卸载windows服务)

    引用地址: https://docs.microsoft.com/zh-cn/dotnet/framework/windows-services/how-to-create-windows-servi ...

  4. 将控制台程序做成windows服务

    一:添加windows服务 二:修改XXXInstaller1的StartType=Automatic,修改ProcessInstaller1的Account=LocalSystem 三:在progr ...

  5. Python读取ini配置文件的方式

    python configparser模块   ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  6. C#开发windows服务如何调试——资料整理

    原文标题:C# Windows服务程序如何进行调试 原文地址:https://jingyan.baidu.com/article/456c463b18e1b00a583144b3.html 第一种: ...

  7. vs2019+windows服务+nancy+打包

    一.创建windows服务  二.nuget包添加nancy 1.nancy 2.0.0和Nancy.Hosting.Self 2.0.0插件 2.项目添加文件夹Modules,在Modules文件夹 ...

  8. C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)

    系列一: 制作一个可安装.可启动.可停止.可卸载的Windows service(downmoon原创) 系列二:演示一个定期执行的windows服务及调试(windows service)(down ...

  9. 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq

    常量,字段,构造方法   常量 1.什么是常量 ​ 常量是值从不变化的符号,在编译之前值就必须确定.编译后,常量值会保存到程序集元数据中.所以,常量必须是编译器识别的基元类型的常量,如:Boolean ...

随机推荐

  1. MySQL超级简明基本操作攻略,给自己看(一)

    系统:Ubuntu 14.04 LTS 安装: apt-get install mysql //安装数据库 apt-get install mysql-workbench //安装图形界面 使用: 启 ...

  2. 怎样訪问pcie整个4k的配置空间

    眼下用于訪问PCIe配置空间寄存器的方法须要追溯到原始的PCI规范. 为了发起PCI总线配置周期,Intel实现的PCI规范使用IO空间的CF8h和CFCh来分别作为索引和数据寄存器,这样的方法能够訪 ...

  3. 133.throw机制 抛出类类型

    #include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...

  4. Python的Flask框架入门-Ubuntu

    全文请见tuts code:An Introduction to Python's Flask Framework Flask是Python一个小而强大的web框架.学起来简单,用起来也容易,能够帮你 ...

  5. ubuntu上配tensorflow

    前一阵绕了大弯路,基本弄好了UEFI双系统后,面对的就是CUDA咋装在Linux. 好在教程好多,有些朋友建议先装CUDA再装显卡驱动.弄好之后记录一下详细过程吧. *** 这两天看了一些教程,还是感 ...

  6. Codeforces 930A. Peculiar apple-tree (dfs)

    题目: 代码: #include <bits\stdc++.h> using namespace std; ]; //b[i]表示距离1号花絮i步的花絮的个数 map <int, l ...

  7. Elasticsearch的Groovy Script自定义评分检索

    需求:以索引中的boostapp列作为评分的基础分值,同时根据carpublishtime(数据的刷新时间字段)按时间进行衰减. 基于Groovy脚本实现. 1.query脚本方式: { " ...

  8. 使用maven插件dockerfile-maven-plugin生成Docker镜像并推送到镜像仓库

    1.引入maven插件 <build> <plugins> <plugin> <groupId>com.spotify</groupId> ...

  9. Django REST framework 自定义字段

    自定义字段 继承 Field 类 覆盖父类中的方法 to_representation() 调用该方法将初始数据类型转换为基本的可序列化数据类型 to_internal_value() 调用该方法将原 ...

  10. mysql 临时表和内存表

    查看内存表的最大值: show variables like '%heap%'; mysql> show variables like '%heap%'; +------------------ ...