Advertisement

04.15.2004 at 02:03AM PDT, ID: 20954883
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

How to write value to config file?

Asked by ryancys in .NET

Tags: , ,

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(getConfigPath, IO.FileMode.Open)
                docConfig.Load(myStream)
                Dim myNode As System.Xml.XmlNode = docConfig.SelectSingleNode("//appSettings/add[@key='" & key & "']")
                myNode.Attributes("value").Value = myNode.Attributes("value").Value & ";P"
                docConfig.Save(getConfigPath)
                'docConfig.Save("c:\test.txt")
                myStream.Close()
                Return True
            End If
        Catch e As Exception
            Console.WriteLine(e.ToString)
            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.LastIndexOfAny(separator)
        intPos += 1
        Return FileFullPath.Substring(intPos, (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.GetExecutingAssembly.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.Reflection.Assembly.GetExecutingAssembly.CodeBase.ToString(), "/")
    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("constring"))

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.WinIOError(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(String filename, Encoding encoding)
   at System.Xml.XmlDOMTextWriter..ctor(String filename, Encoding encoding)
   at System.Xml.XmlDocument.Save(String filename)
   at core.modScripts.SetConfigValue(String key) in H:\DotNet Projects\core\modScripts.vb:line 64
'core.exe': Loaded 'c:\winnt\assembly\gac\system.data\1.0.3300.0__b77a5c561934e089\system.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? thksStart Free Trial
[+][-]04.15.2004 at 02:40AM PDT, ID: 10831508

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.15.2004 at 02:45AM PDT, ID: 10831537

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: .NET
Tags: file, write, config
Sign Up Now!
Solution Provided By: culshaja
Participating Experts: 5
Solution Grade: A
 
 
[+][-]04.15.2004 at 02:58AM PDT, ID: 10831594

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.15.2004 at 03:15AM PDT, ID: 10831661

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.15.2004 at 04:50AM PDT, ID: 10832161

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.15.2004 at 06:00AM PDT, ID: 10832699

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.15.2004 at 10:49AM PDT, ID: 10835375

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42