0

我正在开发一个应用程序作为 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'.

我尝试在控制台应用程序而不是服务应用程序中使用相同的代码,它工作正常。检索到的路径正确,XML文件创建成功C:\TEMP\AMOS_ESDARA

我错过了什么?提前致谢。

4

1 回答 1

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
于 2013-11-13T15:16:53.340 回答