How do I update Webconfig file (C# .Net 2.0) from a Windows CE app

EAK31640GW
EAK31640GW used Ask the Experts™
on
Is there any way to update the Webconfig file during runtime?  
I need to set the application to point to a specific URL which will Validate user credentials and return a URL which will then be used to set the URL for future use.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You cannot see the webconfig file in windows mobile and CE applications. Because they are embedded in your exe file.
the way i can advise  is:
1-Create a registery key for storing your URL.
2-When starting your appication get URL from registery and put it in a global variable.
3- When calling web service in your application set its URL with the global variable which contains the registery.
4-when your URL changes just change the registery value.

The class for editing regisrtey keys is below:


 
Imports System
Imports System.Runtime.InteropServices
Imports System.Text

Public Class clsRegistry
    Public Shared Function CreateKey(ByVal keyName As String) As Integer
        Dim hkey As IntPtr = IntPtr.Zero
        Dim disposition As Integer = 0
        Try
            Return RegCreateKeyEx(HKLM, keyName, 0, Nothing, 0, KeyAccess.None, IntPtr.Zero, hkey, disposition)
        Finally
            If IntPtr.Zero.ToInt32() <> hkey.ToInt32() Then
                RegCloseKey(hkey)
            End If
        End Try
    End Function

    Public Shared Function DeleteKey(ByVal keyName As String) As Integer
        Return RegDeleteKey(HKLM, keyName)
    End Function

    Public Shared Function CreateValueString(ByVal keyName As String, ByVal valueName As String, ByVal stringData As String) As Integer
        Dim hkey As IntPtr = IntPtr.Zero
        Try
            Dim result As Integer = RegOpenKeyEx(HKLM, keyName, 0, KeyAccess.None, hkey)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            Dim bytes As Byte() = Encoding.Unicode.GetBytes(stringData)
            Return RegSetValueEx(hkey, valueName, 0, KeyType.StringKey, _
            bytes, bytes.Length)
        Finally
            If IntPtr.Zero.ToInt32() <> hkey.ToInt32() Then
                RegCloseKey(hkey)
            End If
        End Try
    End Function

    Public Shared Function CreateValueDWORD(ByVal keyName As String, ByVal valueName As String, ByVal dwordData As Integer) As Integer
        Dim hkey As IntPtr = IntPtr.Zero
        Try
            Dim result As Integer = RegOpenKeyEx(HKLM, keyName, 0, KeyAccess.None, hkey)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            Dim bytes As Byte() = BitConverter.GetBytes(dwordData)
            Return RegSetValueEx(hkey, valueName, 0, KeyType.DwordKey, bytes, bytes.Length)
        Finally
            If IntPtr.Zero.ToInt32() <> hkey.ToInt32() Then
                RegCloseKey(hkey)
            End If
        End Try
    End Function

    Public Shared Function GetStringValue(ByVal keyName As String, ByVal valueName As String,ByVal upper As Boolean, ByRef stringResult As String) As Integer
        Dim hkey As IntPtr = IntPtr.Zero
        Dim str As String
        Try
            Dim result As Integer = RegOpenKeyEx(HKLM, keyName, 0, KeyAccess.None, hkey)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            Dim bytes As Byte() = Nothing
            Dim length As Integer = 0
            Dim keyType As KeyType = keyType.NoKey
            result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, keyType, Nothing, length)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            keyType = keyType.NoKey
            bytes = New Byte(length) {}
            result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, keyType, bytes, length)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            str = Encoding.Unicode.GetString(bytes, 0, bytes.Length)
            if(upper= True)
                stringResult = str.Trim.ToUpper.Replace(Chr(0), "") & "" 'deger chr(0) ile geliyor.
            Else
                stringResult = str.Trim.Replace(Chr(0), "") & ""
            End If
            Return ERROR_SUCCESS
        Finally
            If IntPtr.Zero.ToInt32() <> hkey.ToInt32() Then
                RegCloseKey(hkey)
            End If
        End Try
    End Function

    Public Shared Function GetDWORDValue(ByVal keyName As String, ByVal valueName As String, ByRef dwordResult As Integer) As Integer
        Dim hkey As IntPtr = IntPtr.Zero
        Try
            Dim result As Integer = RegOpenKeyEx(HKLM, keyName, 0, KeyAccess.None, hkey)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            Dim bytes As Byte() = Nothing
            Dim length As Integer = 0
            Dim keyType As KeyType = keyType.NoKey
            result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, keyType, Nothing, length)
            bytes = New Byte(Marshal.SizeOf(GetType(System.Int32))) {}
            length = bytes.Length
            keyType = keyType.NoKey
            result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, keyType, bytes, length)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            dwordResult = BitConverter.ToInt32(bytes, 0)
            Return ERROR_SUCCESS
        Finally
            If IntPtr.Zero.ToInt32() <> hkey.ToInt32() Then
                RegCloseKey(hkey)
            End If
        End Try
    End Function

    Public Shared Function DeleteValue(ByVal keyName As String, ByVal valueName As String) As Integer
        Dim hkey As IntPtr = IntPtr.Zero
        Try
            Dim result As Integer = RegOpenKeyEx(HKLM, keyName, 0, KeyAccess.None, hkey)
            If ERROR_SUCCESS <> result Then
                Return result
            End If
            Return RegDeleteValue(hkey, valueName)
        Finally
            If IntPtr.Zero.ToInt32() <> hkey.ToInt32() Then
                RegCloseKey(hkey)
            End If
        End Try
    End Function

    Public Enum KeyType
        NoKey = 0
        StringKey = 1
        DwordKey = 4
    End Enum

    Public Enum KeyAccess
        None = &H0
        QueryValue = &H1
        SetValue = &H2
        CreateSubKey = &H4
        EnumerateSubKeys = &H8
        Notify = &H10
        CreateLink = &H20
    End Enum

    Dim HKCR As New IntPtr(&H80000000)

    Public Shared HKCU As New IntPtr(&H80000001)

    Public Shared HKLM As New IntPtr(&H80000002)

    Public Shared HKU As New IntPtr(&H80000003)

    Public Const ERROR_SUCCESS As Integer = 0

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegCreateKeyEx(ByVal hkey As IntPtr, ByVal lpSubKey As String, ByVal Reserved As Integer, ByVal lpClass As StringBuilder, ByVal dwOptions As Integer, ByVal samDesired As KeyAccess, ByVal lpSecurityAttributes As IntPtr, ByRef phkResult As IntPtr, ByRef lpdwDisposition As Integer) As Integer
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegDeleteKey(ByVal hkey As IntPtr, ByVal subkeyName As String) As Integer
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegOpenKeyEx(ByVal hkey As IntPtr, ByVal lpSubKey As String, ByVal ulOptions As Integer, ByVal samDesired As KeyAccess, ByRef phkResult As IntPtr) As Integer
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegQueryValueEx(ByVal hkey As IntPtr, ByVal lpValueName As String, ByVal lpReserved As IntPtr, ByRef lpType As KeyType, ByVal lpData() As Byte, ByRef lpcbData As Integer) As Integer
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegSetValueEx(ByVal hkey As IntPtr, ByVal lpValueName As String, ByVal Reserved As Integer, ByVal dwType As KeyType, ByVal lpData() As Byte, ByVal cbData As Integer) As Integer
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegDeleteValue(ByVal hkey As IntPtr, ByVal valueName As String) As Integer
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function RegCloseKey(ByVal hkey As IntPtr) As Integer
    End Function

End Class

Open in new window

Author

Commented:
The Question asked for C# the solution was in VB

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial