【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 ...
随机推荐
- Educational Codeforces Round 6 A. Professor GukiZ's Robot 水
A. Professor GukiZ's Robot Professor GukiZ makes a new robot. The robot are in the point with coor ...
- 51nod-1253: Kundu and Tree
[传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...
- 关于AJAX异步请求的那些事儿(1)
1.什么事AJAX? Asynchronous Javascript And XML:异步的JS和XML,由Google2002年在GoogleSuggest应用提出,目标实现客户端和服务器“同时”运 ...
- MyBatis数据持久化(七)多表连接查询
本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...
- (转载)Mac系统下利用ADB命令连接android手机并进行文件操作
Mac系统下利用ADB命令连接android手机并进行文件操作 标签: Mac adb android 2016-03-14 10:09 5470人阅读 评论(1) 收藏 举报 分类: Androi ...
- Caffe学习--Layer分析
Caffe_Layer 1.基本数据结构 //Layer层主要的的参数 LayerParamter layer_param_; // protobuf内的layer参数 vector<share ...
- SQL SERVER 提取字符串中汉字
IF OBJECT_ID('DBO.GET_ZNSTR') IS NOT NULLDROP FUNCTION DBO.GET_ZNSTRGOCREATE FUNCTION DBO.GET_ZNSTR( ...
- 函数签名与消息转发:NSInvocation与NSMethodSignature
具体可见 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Article ...
- 我的Java历程_写出这个数
lzJava基础进行中,今天偶然间看到的一个题目: 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字.如下代码: import java.util.*;public class Ma ...
- element-ui的table表格控件表头与内容列不对齐问题
原文链接:点我 element-ui的table表格控件表头与内容列不对齐问题 解决方法:将以下样式代码添加到index.html.或app.vue中(必须是入口文件,起全局作用!)body .el- ...