我正在开发一个作为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成功创建的。
我错过了什么?提前谢谢。
发布于 2013-11-13 15:16:53
解决了。
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 Functionhttps://stackoverflow.com/questions/19956219
复制相似问题