|
-
Apr 5th, 2007, 06:32 AM
#1
No app.config on a PPC?
Seems like there is no place in the framework for application settings on a PPC app using VS2005.
I'm about to develop my own class to handle this - writing a .ini file - yuk...
Has anyone overcome this with a better method - registry like - or something like that?
-
Apr 5th, 2007, 08:54 AM
#2
Frenzied Member
Re: No app.config on a PPC?
Hi,
I just use an XML file and have routines to read and write.
Format is
<BackupTo>SD Card</BackupTo>
etc.
Peter Foot (www.inthehand.com) has a library that supports app.config, plus many other facilities
Pete
-
Apr 5th, 2007, 09:08 AM
#3
Re: No app.config on a PPC?
Thanks - well since I have to read and write this file myself I mine as well use XML.
Are there XML classes in VS 2005? I only have two or three values to store - so I certainly can do the parsing myself.
Where do you store the .XML file?
-
Apr 5th, 2007, 09:23 AM
#4
Frenzied Member
Re: No app.config on a PPC?
Hi,
I store the XML file in the same folder as the program.
Code as below, used like....
Dim path as string = Get_From_Config_File("PPC_Folder")
vb Code:
Public Function Get_From_Config_File(ByVal strEle As String) As String
Current_Position("Get_From_Config_File " & strEle)
Dim dr As XmlTextReader
'Dim appPath As String
'appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim fs As FileStream = New FileStream(Path.Combine(DataPath, "Config.xml"), FileMode.Open)
Dim strRet As String
dr = New XmlTextReader(fs)
While dr.Read
If dr.NodeType = XmlNodeType.Element And dr.Name = strEle Then
strRet = dr.ReadElementString()
dr.Close()
fs.Close()
Return strRet
End If
End While
dr.Close()
fs.Close()
End Function
Public Function Write_To_Config_File(ByVal strEle As String, ByVal strVal As String) As String
Current_Position("Write_To_Config_File")
Dim iCt As Integer
Dim bDone As Boolean
'Dim appPath As String
'appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim xd As XmlDocument = New XmlDocument
xd.Load(Path.Combine(DataPath, "Config.xml"))
Dim xe As XmlElement = xd.DocumentElement
If xe.Name = "Configuration_Data" Then
For Each xce As XmlElement In xe.ChildNodes
If xce.ChildNodes.Count = 1 Then
If xce.Name = strEle Then
xce.FirstChild.Value = strVal
bDone = True
End If
Else
For iCt = 0 To xce.ChildNodes.Count - 1
If xce.ChildNodes(iCt).Name = strEle Then
xce.ChildNodes(iCt).FirstChild.Value = strVal
bDone = True
End If
Next
End If
If bDone Then
Exit For
End If
Next
End If
' write the document back to disk
xd.Save(Path.Combine(DataPath, "Config.xml"))
End Function
HTH
Pete
-
Apr 5th, 2007, 09:25 AM
#5
Frenzied Member
Re: No app.config on a PPC?
Hi,
sample config file is...
<Configuration_Data>
<Device>XDA</Device>
<Distance>250</Distance>
<Time>30</Time>
<AllowExit>Y</AllowExit>
</Configuration_Data>
etc etc
Pete
-
Apr 5th, 2007, 09:31 AM
#6
Re: No app.config on a PPC?
Thank you - thank you - so very much.
That would have taken me an hour of reading through the VS doc to find all that code!
-
Apr 5th, 2007, 09:35 AM
#7
Frenzied Member
Re: No app.config on a PPC?
Hi,
just realised, you will have to remove the 'current position' stuff, and work out your path, but apart from that you should be good to go
Pete
-
Apr 5th, 2007, 09:39 AM
#8
Re: No app.config on a PPC?
I've been using \My Documents\Business\APC as a folder - APC being the name of the APP.
Is it more appropriate to bury this in program file somewhere??
What the standard on a PPC?
-
Apr 5th, 2007, 09:41 AM
#9
Frenzied Member
Re: No app.config on a PPC?
Hi,
\Program Files\Company-Prod Name\
then your programs.
That is where it will go if you build a deployment package
Pete
-
Apr 5th, 2007, 11:03 AM
#10
Re: No app.config on a PPC?
I don't do a lot of VS work yet - but I took your sample and wrapped it up in a class and also added support for creating the CONFIG.XML file and also adding new nodes to it.
If you see any poor-VS code please comment!
vb Code:
Imports System
Imports System.IO
Imports System.Xml
Public Class Settings
Private strServer As String
Private strUserId As String
Const Datapath = "\Program Files\APC"
Public Property sServer() As String
Get
Return strServer
End Get
Set(ByVal value As String)
strServer = Write_To_Config_File("Server", value)
End Set
End Property
Public Property sUserId() As String
Get
Return strUserId
End Get
Set(ByVal value As String)
strUserId = Write_To_Config_File("UserId", value)
End Set
End Property
Public Sub New()
strServer = Get_From_Config_File("Server")
strUserId = Get_From_Config_File("UserId")
End Sub
Public Function Get_From_Config_File(ByVal strEle As String) As String
Try
Dim dr As XmlTextReader
Dim fs As FileStream = New FileStream(Path.Combine(Datapath, "Config.xml"), FileMode.Open)
Dim strRet As String
strRet = ""
dr = New XmlTextReader(fs)
While dr.Read
If dr.NodeType = XmlNodeType.Element And dr.Name = strEle Then
strRet = dr.ReadElementString()
dr.Close()
fs.Close()
End If
End While
dr.Close()
fs.Close()
Return strRet
Catch ex As FileNotFoundException
Using sw As StreamWriter = New StreamWriter(Path.Combine(Datapath, "Config.xml"))
sw.WriteLine("<Configuration_Data>")
sw.WriteLine("</Configuration_Data>")
sw.Close()
End Using
Return ""
Catch ex As Exception
Return ""
End Try
End Function
Public Function Write_To_Config_File(ByVal strEle As String, ByVal strVal As String) As String
Write_To_Config_File = ""
Dim iCt As Integer
Dim bDone As Boolean
'Dim appPath As String
'appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim xd As XmlDocument = New XmlDocument
xd.Load(Path.Combine(Datapath, "Config.xml"))
Dim xe As XmlElement = xd.DocumentElement
If xe.Name = "Configuration_Data" Then
For Each xce As XmlElement In xe.ChildNodes
If xce.ChildNodes.Count = 1 Then
If xce.Name = strEle Then
xce.FirstChild.Value = strVal
bDone = True
End If
Else
For iCt = 0 To xce.ChildNodes.Count - 1
If xce.ChildNodes(iCt).Name = strEle Then
xce.ChildNodes(iCt).FirstChild.Value = strVal
bDone = True
End If
Next
End If
If bDone Then
Exit For
End If
Next
If Not bDone Then
Dim elem As XmlElement = xd.CreateElement(strEle)
elem.InnerText = strVal
xe.AppendChild(elem)
End If
End If
' write the document back to disk
xd.Save(Path.Combine(Datapath, "Config.xml"))
Write_To_Config_File = strVal
End Function
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|