hey i am currently converting my functions from console program to windows service program and have trouble with reading my settings.xml file and write the log file.
how do i specify where the program will look for the files? it would be great if it could work like in my console version when the program looks in the map from where it is started from.
can you do this in windows service program to look in the map from where i installed the service?
This is how the functions look like right now
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;
}
And the other
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
}
how will i make my service know where the files are? :) thank you for answers
EDIT
[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);
}
}
this is my installer if that helps