0

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

Dendei
  • 551
  • 1
  • 7
  • 18
  • 1
    please see my previous question: http://stackoverflow.com/questions/6544437/how-can-i-set-sort-of-start-in-path-for-a-windows-service –  Dec 03 '12 at 15:15
  • hmm yes thank you :) but i think the @"C:\Documents and Settings\All Users\ approach makes more sense to me :P – Dendei Dec 03 '12 at 15:42

1 Answers1

1

how do i specify where the program will look for the files?

.NET looks for files in Environment.CurrentDirectory if no path was specified. The directory depends on how the application was started.

If you for instance run the application from within Visual Studio the folder will be the Output Path (under project settings / build), while for windows services it will be the directory that the service exists in.

Windows services are started from the folder where the Service Control Manager is located (source: What directory does a Windows Service run in?).

You can find your own directory by using Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • added the installer class above but how do i know where the services currentDirectory is when i install it like this? cant i make it the Directory where my .exe is? – Dendei Dec 03 '12 at 15:14
  • @Dendei: läs min uppdatering – jgauffin Dec 03 '12 at 15:15
  • 1
    @Dendei it's a very bad idea to rely on current directory and on relative paths. Even Microsoft itself doesn't recomment using Current Directory value. You should use one of predefined directories under Users\All users (for example) for storing your settings. – Eugene Mayevski 'Callback Dec 03 '12 at 15:16
  • @EugeneMayevski'EldoSCorp makes sense :) but how do i specify the path? do i use "@C:\Documents and Settings\All Users\Settings.xml" to create it, at my specified location? – Dendei Dec 03 '12 at 15:25