Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello all,

 

I want to store a connectionstring, wich will be used to make

the dataAdapter for accessing the 'main' database.

The connectionstring is a normal string.

 

This data must be kept, even when the program or

computer shuts down!

 

I do not want to keep the string in a database, because

for accessing this database I need a connectionstring first,

and thats just NOT what I wanted!

 

I can store this sting in the register with the getsetting,

loadsetting and deletesetting.

 

QUESTION:

NOW I am wondering, isn't there onother way of keeping

data?

 

 

 

 

thanks,

reinier

Posted

Hello plausibledamp,

 

It is not a web app, can you give me another link

for a app.config file,

and can you tell me what the advantage or disadvantage

is, comparing it with the windows registry?

 

 

thank you again,

 

reinier

  • Administrators
Posted

Registry can only be edit with certain tools, usually it isn't cleaned up after an application is un-installed, if you move your app to another machine then it's hard to get registry settings to move as well.

 

The little sample attached should give you an idea:

Note the name of the file under VS is called app.config, however at runtime the file needs to be called (yourappname.exe.config - VS takes care of this for you).

 

Build the sample and go to the bin directory generated and run the exe, click the button and see what the label is set to.

Edit the .config file in any text editor and click the button again (remembering to save the file!)

appconfig.zip

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

storing data NOt in a database

 

PlausiblyDamp,

 

The zipped prog work ok!

This makes thing clear.

 

The only thing is, how do I change

a setting in the config file, by source code.

 

Sofar, when I change the string manually,

your prog reads it....perfect!

However, I want to control it by source code,

is there a way of doing this, lets say instead of

getvalue, id there something like savevalue???

 

Best regards!!

 

reinier

Edited by reinier
  • Administrators
Posted

There are ways round it but by design the .config file is really for admin use, if your app is running under XP or later (not sure of win2k) then a normal user will not have write privelleges to the apps install folder.

If you wish to store user settings then saving the info to a XML file using XMLSerialisation is pretty easy.

This file can be stored as part of the users profile by saving it in a sub-folder of the path returned by

system.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

this will allow every user to have their own settings or by using

System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

information can be shared by all users.

You could also store information in all 3 places depending on if it's admin stuff (.config), individual user info (ApplicationData) or shared config (CommonApplicationData)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

PlausiblyDamp,

 

I looked your code, referring to XMLSerialisation,

and seems pretty complicated.

I think when it IS possible to make a configfile at

individual user - level, that this would work ok.

 

And ofcourse I can use the window reg...

 

OR

 

make a .ini file (how? I do not know).

 

 

 

(For now I gonna trie to make config file on a

ind. user level)

 

Best regards,

reinier

  • Administrators
Posted

given a class like

Public Class UserConfig
	 Public val1 As Integer = 1
	 Public val2 As String = "Hello"
	 Public val3 As Date = Now

End Class

 

it could be serialized with code like

	'to save
	Dim f As New System.IO.FileStream("c:\test.xml", IO.FileMode.Create)
	Dim x As New System.Xml.Serialization.XmlSerializer(GetType(UserConfig))
	Dim u As New UserConfig
	x.Serialize(f, u)
	f.Close()

'to load
	Dim f2 As New System.IO.FileStream("c:\test.xml", IO.FileMode.Open)
	Dim x2 As New System.Xml.Serialization.XmlSerializer(GetType(UserConfig))
	Dim u2 As UserConfig
	u2 = x.Deserialize(f2)
	f.Close()

 

just store it in the places I mentioned above rather than the c:\test.xml I've hard-coded

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Have you thought about storing it in an XML config file?

 

The examples below are from a scheduler I wrote to run once a day, as the Windows Task scheduler did not have permission to run files on some machines.

 

Reading into a ListView control:

'Read the XML document
objXMLDoc = New XmlDocument
objXMLDoc.Load(Application.StartupPath & "\Tasks.xml")

'Extract the Tasks
lstTasks.Items.Clear()
For i = 0 To objXMLDoc.DocumentElement.ChildNodes.Count - 1
   With objXMLDoc.DocumentElement.ChildNodes(i)
       'Create the listitem and add its columns from the XML document
       lstItem = New ListViewItem
       lstItem.Text = .Attributes("TaskName").Value()
       lstItem.SubItems.Add(.Attributes("NextRunTime").Value())
       lstItem.SubItems.Add(.Attributes("LastRunTime").Value())
       lstItem.SubItems.Add(.Attributes("TaskToRun").Value())

       'Add the listitem to the list
       lstTasks.Items.Add(lstItem)
   End With
Next

Writing into the XML file

objXMLDoc = New XmlDocument

'Initialise the xml file for writing
objXMLWriter = New XmlTextWriter(Application.StartupPath & "\Tasks.xml", Nothing)
With objXMLWriter
   .Formatting = Formatting.Indented
   'add the XML Header
   .WriteStartDocument()
   'Create the Tasks Node
   .WriteStartElement("Tasks")

   'Extract the Tasks into Task nodes
   For i = 0 To lstTasks.Items.Count - 1
       'Create the Task Node
       .WriteStartElement("Task")
       'Add the Attributes required
       .WriteAttributeString("TaskName", lstTasks.Items(i).Text)
       .WriteAttributeString("NextRunTime", lstTasks.Items(i).SubItems(1).Text)
       .WriteAttributeString("LastRunTime", lstTasks.Items(i).SubItems(2).Text)
       .WriteAttributeString("TaskToRun", lstTasks.Items(i).SubItems(3).Text)
       'End the Task Node
       .WriteEndElement()
   Next

   'End the Tasks Node
   .WriteEndElement()
   .WriteEndDocument()
End With

'Save the XML configuration file
objXMLDoc.Save(objXMLWriter)
objXMLWriter.Close()

And this is the XMl I read to and write from:

<?xml version="1.0" encoding="utf-8" ?> 
<Tasks>
 <Task TaskName="Daily Task 1" NextRunTime="1/7/2004 10:05:40 AM" LastRunTime="1/6/2004 12:00:00 PM" TaskToRun="C:\WINNT\system32\calc.exe" /> 
 <Task TaskName="Daily Task 2" NextRunTime="1/7/2004 3:00:00 PM" LastRunTime="1/6/2004 3:00:00 PM" TaskToRun="C:\WINNT\system32\calc.exe" /> 
</Tasks>

Posted

PlausiblyDamp,

 

I implemented your code succesfully in a

new project.

The data is saved to an xml file,

(if it is returned I do'nt know, I cant check that)

 

I feel like a stupid newbie now, but how can

I SAVE (for example txtsave) the text of a textbox,

to the xmlfile,

 

 

 

and READ the content of the XMLfile to

(for example an label: me.lblread)???

 

 

regards,

reinier:D :confused:

Posted (edited)

SOLUTION: data not stored on a database

 

solution: ( ALL the thanks goto PlausiblyDamp )

use an xmlfile.

this is the code!

I used one button to write to the xml file,

another button ti read the xml file.

 

also see url: http://support.microsoft.com/defaul...kb;en-us;316730

 

good luck for someone who needs an XML-file/XMLfile !

 

coding of the form:

Imports System.Xml.Serialization

Imports System.IO

Public Class Form1

Inherits System.Windows.Forms.Form

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

''Write(Me.txtName, Me.txtDescription, Me.txtQuantity)

Dim p As New clsProduct

 

p.Name = Me.txtName.Text

p.Description = Me.txtDescription.Text

p.Qty = Me.txtQuantity.Text

 

 

 

'--waarden in xml wegzetten

'Serialize object to a text file.

Dim objStreamWriter As New StreamWriter("D:\Documents and Settings\Administrator\Mijn documenten\Visual Studio Projects\WindowsApplication52\test.xml")

Dim x As New XmlSerializer(p.GetType)

x.Serialize(objStreamWriter, p)

objStreamWriter.Close()

 

 

 

 

 

 

''MsgBox("klaar")

End Sub

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim p As New clsProduct

 

 

 

 

''--waarden in xml wegzetten

Dim x As New XmlSerializer(p.GetType)

 

 

 

''--xml uitlezen

'Deserialize text file to a new object.

Dim objStreamReader As New StreamReader("D:\Documents and Settings\Administrator\Mijn documenten\Visual Studio Projects\WindowsApplication52\test.xml")

Dim p2 As New clsProduct

p2 = x.Deserialize(objStreamReader)

objStreamReader.Close()

 

Me.lblNaam.Text = p2.Name

Me.lblDescription.Text = p2.Description

Me.lblQuantity.Text = CStr(p2.Qty)

End Sub

End Class

 

 

 

this is the coding of class clsProduct:

 

Public Class clsProduct

Private mstrName As String

Private mstrDescription As String

Private mintQty As String

 

Public Property Name() As String

Get

Name = mstrName

End Get

Set(ByVal Value As String)

mstrName = Value

End Set

End Property

 

Public Property Description() As String

Get

Description = mstrDescription

End Get

Set(ByVal Value As String)

mstrDescription = Value

End Set

End Property

 

Public Property Qty() As String

Get

Qty = mintQty

End Get

Set(ByVal Value As String)

mintQty = Value

End Set

End Property

Edited by reinier

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...