Windows服务 - 检索错误的AppConfig密钥

时间:2013-11-13 14:24:27

标签: vb.net service path app-config

我正在开发一个应用程序作为Windows服务。该服务从app.config检索路径,但由于某种原因,检索到的路径的一部分在执行期间发生了变化,并替换为C:\Windows\System32

这是我的app.config

[...]
  <appSettings>
    <add key="Freq_Minutes" value="1" />
    <add key="Connectionstring" value="Server=FENIX\SQL2005;Database=amoselprat;Uid=amos;Pwd=mosa;"/>
    <add key="AMOSEsdara_Path" value="C:\TEMP\AMOS_ESDARA"/>
    <add key="EsdaraAMOS_Path" value="C:\TEMP\ESDARA_AMOS"/>
  </appSettings >
[...]

这是检索密钥的函数

Public Function GetInfo(ByVal Label As String) As String
    Dim Value As String

    Try
        Value = System.Configuration.ConfigurationManager.AppSettings(Label).ToString

    Catch ex As Exception
        Value = Nothing
    End Try
    Return Value

End Function

这是有问题的代码

    Public Sub Components(ByVal AutoNumber As String)

        Dim sw As StreamWriter
        Dim File As String

        File = GetInfo("AMOSEsdara_Path") & "\AMOS_ESDA_COMP_" & Autonumber & ".xml"

        Try

            EventLog_AMOSEsdara.WriteEntry("AMOSEsdara Interface - Creating components file " & File)
            sw = File.CreateText(File)

[...]

    Catch Ex As Exception

        EventLog_AMOSEsdara.WriteEntry("AMOSEsdara Interface - Error creating file " & File & " Error: " & Ex.Message)

    End Try

End Sub

作为服务运行EventLog正在编写以下错误:

AMOSEsdara Interface - Error creating file AMOS_ESDARA\AMOS_ESDA_COMP_000006.xml Error: Can not find a part of the path 'C:\Windows\system32\AMOS_ESDARA\AMOS_ESDA_COMP_000006.xml'.

我尝试将相同的代码用于控制台应用程序而不是服务应用程序,它运行正常。检索到的路径正确,并且在C:\TEMP\AMOS_ESDARA

成功创建了XML文件

我缺少什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

解决。

    Public Function GetInfo(ByVal Label As String) As String
        Dim Value As String


    System.IO.Directory.SetCurrentDirectory((System.AppDomain.CurrentDomain.BaseDirectory))

        Try
            Value = System.Configuration.ConfigurationManager.AppSettings(Label).ToString

        Catch ex As Exception
            Value = Nothing
        End Try
        Return Value

End Function