【C#Windows 服务】 《二》INI配置文件
一、工具:
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配置文件的更多相关文章
- Windows服务二:测试新建的服务、调试Windows服务
一.测试Windows服务 为了使Windows服务程序能够正常运行,我们需要像创建一般应用程序那样为它创建一个程序的入口点.像其他应用程序一样,Windows服务也是在Program.cs的Main ...
- WCF寄宿windows服务二
如果有很多WCF服务需要寄宿,需要额外做一些工作:总体思路是:先把这些WCF服务的程序集打包,然后利用反射加载各个WCF服务的程序集,按顺序一个一个寄宿.先来看看我们需要寄宿的WCF服务: 实现步骤: ...
- C#创建windows服务(二:创建和卸载windows服务)
引用地址: https://docs.microsoft.com/zh-cn/dotnet/framework/windows-services/how-to-create-windows-servi ...
- 将控制台程序做成windows服务
一:添加windows服务 二:修改XXXInstaller1的StartType=Automatic,修改ProcessInstaller1的Account=LocalSystem 三:在progr ...
- Python读取ini配置文件的方式
python configparser模块 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- C#开发windows服务如何调试——资料整理
原文标题:C# Windows服务程序如何进行调试 原文地址:https://jingyan.baidu.com/article/456c463b18e1b00a583144b3.html 第一种: ...
- vs2019+windows服务+nancy+打包
一.创建windows服务 二.nuget包添加nancy 1.nancy 2.0.0和Nancy.Hosting.Self 2.0.0插件 2.项目添加文件夹Modules,在Modules文件夹 ...
- C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)
系列一: 制作一个可安装.可启动.可停止.可卸载的Windows service(downmoon原创) 系列二:演示一个定期执行的windows服务及调试(windows service)(down ...
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
常量,字段,构造方法 常量 1.什么是常量 常量是值从不变化的符号,在编译之前值就必须确定.编译后,常量值会保存到程序集元数据中.所以,常量必须是编译器识别的基元类型的常量,如:Boolean ...
随机推荐
- App 签名过期或泄露怎么办?别担心,Google 已经给出解决方案!
一.序 在将 App 发布到市场之前,很重要的一个步骤就是为 APK 进行签名,大部分时候,这个操作隐藏在了打包的流程中,而不被我们注意到. 签名的作用,除了证明 App 的所有权之外,还可以帮助 A ...
- Java推断类和实例的关系
通常我们使用instanceOf关键字来推断一个对象是否是类的实例,近期博主看到isInstance关键字,不解与instanceOf的差别,故度娘了一下,顺便涨了一下姿势. Java中推 ...
- Codeforces 987B. High School: Become Human
解题思路: 1.题意:判断x^y和y^x谁大谁小. 2.由于x^y和y^x太大了,时间复杂度也不允许,所以做同等变换,比较e^(ylnx)和e^(xlny). 3.即为比较ylnx和xlny的大小. ...
- PHP mysql_fetch_array得不到数据
好久没写PHP了...为了数据库课设开始了PHP速成之旅... 光是简单的注册功能就坑了我好几个小时,不过万事开头难嘛...之后应该会快起来的. 先说一下昨天遇到的小坑, 我的表单是这么写的 < ...
- Github上值得关注的前端项目-转自好友trigkit4
http://microjs.com/# 该网站的资源都托管到了github,microjs.com是一个可以让你选择微型的js类库的网站,该网站里的js库都是压缩后不大于5KB的,非常实用 http ...
- php对excel导入导出的支持
闲话不多说了,大家直接进入主题 php对excel的导入: 1.为程序添加一个form表单,为form标签添加“enctype="multipart/form-data"”属性 ...
- pandas学习笔记 - 常见的数据处理方式
1.缺失值处理 - 拉格朗日插值法 input_file数据文件内容(存在部分缺失值): from scipy.interpolate import lagrange import pandas as ...
- 题解 P3372 【【模板】线段树1 】(zkw)
看了一下题解里的zkw线段树,感觉讲的不是很清楚啊(可能有清楚的但是我没翻到,望大佬勿怪). 决定自己写一篇...希望大家能看明白... zkw线段树是一种优秀的非递归线段树,速度比普通线段树快两道三 ...
- 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)
Description 存在如下递推式: F(n+1)=A1*F(n)+A2*F(n-1)+-+An*F(1) F(n+2)=A1*F(n+1)+A2*F(n)+-+An*F(2) - 求第K项的值对 ...
- 【转】 值得推荐的C/C++框架和库 (真的很强大)
[转] 值得推荐的C/C++框架和库 (真的很强大) 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个 ...