C# 如何在Windows服务应用程序中添加config.ini文件?
我已经安装了这个应用程序,但问题是这个应用程序没有读取config.ini文件,我得到错误,config.ini不存在?确定我错了,我不知道如何在安装项目中添加config.ini文件C# 如何在Windows服务应用程序中添加config.ini文件?,c#,c#-4.0,configuration-files,C#,C# 4.0,Configuration Files,我已经安装了这个应用程序,但问题是这个应用程序没有读取config.ini文件,我得到错误,config.ini不存在?确定我错了,我不知道如何在安装项目中添加config.ini文件 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.Ser
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.Timers;
using System.Net.NetworkInformation;
using System.IO;
namespace MovimentoServic
{
public partial class Service1 : ServiceBase
{
Dictionary<string, string> vpN = new Dictionary<string, string>();
cancelaments cancel = new cancelaments();
vendas vendas = new vendas();
Vendaecf ecf = new Vendaecf();
Sangrias sangria = new Sangrias();
Devolucao devolucao = new Devolucao();
CancelamentECF cancelecf = new CancelamentECF();
EnviarItemparalojas enivar = new EnviarItemparalojas();
RecheckVendas recheck = new RecheckVendas();
String vpn = null;
String host = null;
string user = null;
string pass = null;
string mysql_db = null;
string vpn_db = null;
String permissao = null;
int count = 0;
public void conect()
{
if ( System.IO.File.Exists("config.ini") )
{
String[] INI = System.IO.File.ReadAllLines("config.ini");
for ( int i = 0; i < INI.Length; i++ )
{
if ( INI[i].StartsWith("HOST") )
{
host = INI[i].Substring(INI[i].IndexOf("=") + 1);
}
if ( INI[i].StartsWith("USER") )
{
user = INI[i].Substring(INI[i].IndexOf("=") + 1);
}
if ( INI[i].StartsWith("PASS") )
{
pass = INI[i].Substring(INI[i].IndexOf("=") + 1);
}
if ( INI[i].StartsWith("DBPATH") )
{
mysql_db = INI[i].Substring(INI[i].IndexOf("=") + 1);
}
if ( INI[i].StartsWith("VPN") )
{
vpn = (INI[i].Substring(INI[i].IndexOf("=") + 1));
Char[] cs = { ';' };
String[] VPN = vpn.Split(cs);
vpN.Add(VPN[0], VPN[1]);
}
if ( INI[i].StartsWith("DBPATH_VPN") )
{
vpn_db = (INI[i].Substring(INI[i].IndexOf("=") + 1));
}
if ( INI[i].StartsWith("Permissao") )
{
permissao = (INI[i].Substring(INI[i].IndexOf("=") + 1));
}
}
}
}
public static bool IsAlive( string aIP, String shop )
{
bool result = false;
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10;
PingReply reply = pingSender.Send(aIP, timeout, buffer, options);
if ( reply.Status == IPStatus.Success )
{
result = true;
}
return result;
}
long Validtime = 50;
int tk = 0;
private void timer1_Elapsed( object sender, EventArgs e )
{
tk++;
if ( tk == Validtime )
{
// Workout();
tk = 0;
}
}
public Service1()
{
InitializeComponent();
}
protected override void OnStart( string[] args )
{
conect();
Timer timer1 = new Timer();
timer1.Interval = 100;
timer1.Start();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
}
protected override void OnStop()
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统诊断;
使用System.Linq;
使用System.ServiceProcess;
使用系统文本;
使用系统计时器;
使用System.Net.NetworkInformation;
使用System.IO;
名称空间MovimentoServic
{
公共部分类Service1:ServiceBase
{
Dictionary vpN=新字典();
取消取消=新取消();
vendas vendas=新的vendas();
Vendaecf ecf=新的Vendaecf();
桑格里亚斯桑格里亚斯=新桑格里亚斯();
Devolucao Devolucao=新Devolucao();
CancelamentECF cancelecf=新的CancelamentECF();
enviraritemparalojas enivar=新enviraritemparalojas();
RecheckVendas recheck=新的RecheckVendas();
字符串vpn=null;
字符串host=null;
字符串user=null;
字符串传递=null;
字符串mysql_db=null;
字符串vpn_db=null;
字符串permissao=null;
整数计数=0;
公共无效合同()
{
if(System.IO.File.Exists(“config.ini”))
{
字符串[]INI=System.IO.File.ReadAllLines(“config.INI”);
对于(int i=0;i
使用.NET Framework自2.0版以来内置的设置机制,而不是config.ini
文件
要执行此操作,请打开项目的属性并转到Settings
选项卡。添加所需的设置(请注意,正在创建一个app.config
文件,该文件在编译时作为.exe.config
复制到输出文件夹)。在程序中,使用以下设置:
string myStringSetting = Properties.Settings.Default.MySetting
安装服务后,更改
exe.config
文件将使服务在重新启动后使用新设置。您的问题是因为您对ini文件的位置和服务的启动方式做出了假设
在服务中,您不能依赖当前工作目录每次都位于同一位置。您应该将配置文件的路径存储在注册表中(甚至首先将配置数据存储在注册表中)