0

現在、関数をコンソールプログラムから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);
    }
}

それが助けになるなら、これは私のインストーラーです

4

1 に答える 1

1

プログラムがファイルを探す場所を指定するにはどうすればよいですか?

Environment.CurrentDirectoryパスが指定されていない場合、.NETはでファイルを検索します。ディレクトリは、アプリケーションの起動方法によって異なります。

たとえば、Visual Studio内からアプリケーションを実行する場合、フォルダーは(プロジェクト設定/ビルドの下の)出力パスになります。一方、Windowsサービスの場合は、サービスが存在するディレクトリになります。

Windowsサービスは、サービスコントロールマネージャーが配置されているフォルダーから開始されます(ソース:Windowsサービスはどのディレクトリで実行されますか?)。

を使用して独自のディレクトリを見つけることができますPath.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

于 2012-12-03T15:07:00.537 に答える