Hi,
I got problem Writing values to the config file (example: exeName.exe.config), currently i use this code:
'Set Config Key Value
Public Function SetConfigValue(ByVal key As String) As Boolean
Try
If isFileExist(getConfigPath)
Then
Dim docConfig As New System.Xml.XmlDocument()
Dim myStream As New IO.FileStream(getConfigPat
h, IO.FileMode.Open)
docConfig.Load(myStream)
Dim myNode As System.Xml.XmlNode = docConfig.SelectSingleNode
("//appSet
tings/add[
@key='" & key & "']")
myNode.Attributes("value")
.Value = myNode.Attributes("value")
.Value & ";P"
docConfig.Save(getConfigPa
th)
'docConfig.Save("c:\test.t
xt")
myStream.Close()
Return True
End If
Catch e As Exception
Console.WriteLine(e.ToStri
ng)
Return False
End Try
End Function
'Get File Name
Public Function getFileName(ByVal FileFullPath As String, Optional ByVal separator As String = "\") As String
'EXAMPLE: input ="C:\winnt\system32\kernel
.dll,
'output = kernel.dll
Dim intPos As Integer
intPos = FileFullPath.LastIndexOfAn
y(separato
r)
intPos += 1
Return FileFullPath.Substring(int
Pos, (Len(FileFullPath) - intPos))
End Function
'Get Current Exe Config File
Public Function getConfigPath() As String
Return getAppPath() & getEXEName() & ".config"
End Function
'Get Application Path
Public Function getAppPath(Optional ByVal removeSeparator As Boolean = False)
Dim tmp As String = System.Reflection.Assembly
.GetExecut
ingAssembl
y.Location
.ToString
tmp = Replace(tmp, Dir(tmp), "", , , CompareMethod.Text)
If removeSeparator And Right(tmp, 1) = "\" Then tmp = Left(tmp, Len(tmp) - 1)
Return tmp
End Function
'Get Current Exe Name
Public Function getEXEName() As String
Return getFileName(System.Reflect
ion.Assemb
ly.GetExec
utingAssem
bly.CodeBa
se.ToStrin
g(), "/")
End Function
the config file will be like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="constring" value="data source=SERVER;initial catalog=db;user id=xx;password=xxx;persist
security info=True;workstation id=SERVER;packet size=4096"/>
</appSettings>
</configuration>
so i try like:
MsgBox(SetConfigValue("con
string"))
and i got this error:
System.IO.IOException: The process cannot access the file "H:\DotNet Projects\core\bin\core.exe
.config" because it is being used by another process.
at System.IO.__Error.WinIOErr
or(Int32 errorCode, String str)
at System.IO.FileStream..ctor
(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor
(String path, FileMode mode, FileAccess access, FileShare share)
at System.Xml.XmlTextWriter..
ctor(Strin
g filename, Encoding encoding)
at System.Xml.XmlDOMTextWrite
r..ctor(St
ring filename, Encoding encoding)
at System.Xml.XmlDocument.Sav
e(String filename)
at core.modScripts.SetConfigV
alue(Strin
g key) in H:\DotNet Projects\core\modScripts.v
b:line 64
'core.exe': Loaded 'c:\winnt\assembly\gac\sys
tem.data\1
.0.3300.0_
_b77a5c561
934e089\sy
stem.data.
dll', No symbols loaded.
The program '[1984] core.exe' has exited with code 0 (0x0).
>>because it is being used by another process.
How can i overcome this? thks