首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows检索错误的AppConfig密钥

Windows检索错误的AppConfig密钥
EN

Stack Overflow用户
提问于 2013-11-13 14:24:26
回答 1查看 129关注 0票数 0

我正在开发一个作为windows服务的应用程序。服务从app.config检索路径,但由于某种原因,部分检索路径在执行过程中发生更改,并被C:\Windows\System32替换。

这是我的app.config

代码语言:javascript
复制
[...]
  <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 >
[...]

这是检索键的函数。

代码语言:javascript
复制
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

这是麻烦的代码

代码语言:javascript
复制
    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正在写入以下错误:

代码语言:javascript
复制
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成功创建的。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-13 15:16:53

解决了。

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19956219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档