嘿,我目前正在将我的功能从控制台程序转换为 Windows 服务程序,并且无法读取我的 settings.xml 文件并写入日志文件。
我如何指定程序将在哪里查找文件?当程序从它开始的地方查看地图时,如果它可以像我的控制台版本一样工作,那就太好了。
您可以在 Windows 服务程序中执行此操作以查看我安装该服务的地图吗?
这就是现在的功能
public List<string> GetSettingsXml() //Läser settings.xml som innehåller företags sökvägarna som behövs för att öppna kontakten till visma adminitration
{
List<string> PathList = new List<string>();
try
{
XmlReader xr;
using (xr = XmlReader.Create("Settings.xml")) //Läs från Settings.xml
{
while (xr.Read()) //Läser xml filen till slutet
{
if (xr.HasValue) //om den har ett värde så...
{
if ((!String.IsNullOrWhiteSpace(xr.Value)))
{
PathList.Add(xr.Value.ToString()); //plockar ut alla värden i xml filen
}
}
}
}
}
catch (Exception e)
{
WriteToLog("127.0.0.1", "GetSettings", "Exception", e + ": " + e.Message);
}
return PathList;
}
和另一个
public void WriteToLog(string ip, string FunctionName, string Keyfield, string Result) //skapar log filen om den inte finns och skriver i den
{
if (ip != "127.0.0.1")
{
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
ip = endpoint.Address;
}
string Today = DateTime.Today.ToString().Remove(10);
StreamWriter log;
if (!File.Exists("logfile " + Today + ".txt"))
{
log = new StreamWriter("logfile " + Today + ".txt");
}
else
{
log = File.AppendText("logfile " + Today + ".txt");
log.WriteLine();
}
log.Write(DateTime.Now + " ::: " + ip + " ::: Funktions namn: " + FunctionName);
if (Keyfield != "NoKeyField")
log.Write(" ::: Nyckelfält: " + Keyfield + " ::: Resultat: " + Result);
else
log.Write(" ::: Resultat: " + Result);
if (FunctionName == "host.Close()")
log.WriteLine();
log.Close(); //stäng loggen
}
我如何让我的服务知道文件在哪里?:) 谢谢你的回答
编辑
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
/// <summary>
/// Public Constructor for WindowsServiceInstaller.
/// - Put all of your Initialization code here.
/// </summary>
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "VenatoWCF";
serviceInstaller.Description = "VenatoWCF 0.903 skapar en WCF service som med hjälp av olika funktioner kommunicerar mellan Visma Administration och Client";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "VenatoWCF";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
如果有帮助,这是我的安装程序