Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit text file 1

Status
Not open for further replies.

NICKYSUWANDI

Programmer
Jan 8, 2004
80
ID
i am newbie

i had create program to store my config in text file named Myconfig.txt.
i fill this file with:

Backgroundimage = ...
Fonts = ....
Color = ...

i am very confused how to change the text value like (background = c:\image.jpg or fonts = Arial) from program and how to set screen color and fonts using value return from myconfig.txt.
Please someone can give me a solution

Thanks

Nicky



 
Rather than use a text file, consider using a config file and a configreader to read the relevant settings.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Create app.config in you .exe dir

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
	<add key="Background" value="myimg.img" />
	<add key="Font" value="font" />
<appSettings>
<configuration>

use as
Code:
dim strBackground as string
strBackground = System.Configuration.ConfigurationSettings.AppSettings("Background")

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
SMURF75 can u give a detail instruction because when try implement it to my program variabel strBackground = "" (return empty variabel)

Thanks

Nicky
 
Im sorry, but the only thing i can think of right now is to ask you if you have included the file to your project as well. I kinda forgot to mention that ;)


- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
SMURF75, i try to add this code to my program and its working

Dim settings As New System.Configuration.AppSettingsReader
MessageBox.Show(settings.GetValue("Color", GetType(String)))

but 1 had another question:
1. How to modify this setting (app.config) from program. For
example when user change screen color, i want this setting
saved to app.config.
2. how to change screen color or font when i take the value
from app.config.
3. how to change picture location (x,y) using code.

Thanks

Nicky
 
As for number 1 I assume the
Code:
Appsettings.Set(strName, strValue)
would be the right approach. Where i guess name is the key and value is... the value ;) (havnt tried that one tough)

as for number 2 and 3 I think you will have better luck starting another thread since those hasnt got to much to do with the topic of this one :)

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
I would highly, HIGHLY recommend NOT using App.config for user setting.

1) There's only 1 App.Config for an application. If you have multiple users on one system (Shared PCs, Terminal Server/Cytrix) this causes any one person's settings to overwrite the others. This also just happened in my office yestarday. A 3rd party leasing solution uses a settings file i the app folder to determine which environment to log into by default. We realised at 1 in the afternoon that the entire leasing department was logged into the test environment and had wasted half a days work.

2) C:\Program Files\... is often protected. Non-Admins who attempt the run the program will get errors because they do not have sufficient rights to modify the file.

3) App.Config will not roam. If your network has roaming profiles, you can put settings files in the UserSettings folder to have them follow the user from machine to machine.

The way I recommend, is via ADO.Net, XML and the Environment.SpecialFolder.ApplicationData folder.

On loading of the app (ie: under a splash screen) check to see if the XML file exists in Environment.SpecialFolder.ApplicationData. If the file does not exist, create it with default values. Create a dataset, add a data table, add 2 columns to the datatable, a key field and a value field. Add all of the Key-Value pairs you want to the data table. Use the DataSet.WriteXML method to write the data to an XML file in the Environment.SpecialFolder.ApplicationData folder. Then load the XML file. If the user changes any settings, update the data table and use the dataset.writeXML method to update the XML file.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick,

I'm considering using this XML method for an application that I'm writing. However, I would like to take this one step further and somehow encrypt the XML data so that just in case someone does get to the file manually, they will not be able to read the data.

Do you know of any encryption algorithms or procedures I can use to encrypt and decrypt the data? If so I'd appreciate it if you could send me an example.

BTW, I'm a complete newbie to the encryption/security features in .NET. I've looked at other postings of hash tables and other encryption methods, but none of it makes sense to me.

-lucyv
 
My knowledge of encryption is very limited. I know there are encryption libraries built into the framework though. Try gooleing: "VB.Net" encryption sample.

And don't let hash tables scare you. Hash Table is a fancy way of saying an Array of Pairs(A Key and A Value). It's got lots of cool stuff built in (sorting, filtering, searching) but all it is, is a simple array.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
ThatRickGuy, i tried u advised, but i had a problem to edit value in my xml file, can u give the example code how to change value in xml file. (i want user can save settings like backgroupimage, backcolor and font in myconfig.xml).

Thanks

Nicky


 
This is quasi code/psuedo code, but hopefully it'll point you in the right direction.

Code:
dim ds as dataset
ds.readxml([your config.xml file here])
dim dr() as datarow = ds.tables(0).select("Key = '" & KeyValueHere & "'")
dr(0).item("Value") = NewValue
ds.writexml([your config.xml file here])

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
theres error in you code ThatRickGuy :
'---------
Dim ds As DataSet
ds.ReadXml("myconfig.xml") 'error in here

Dim dr() As DataRow = ds.Tables(0).Select("Key = '" & "ISBN" & "'")
dr(0).Item("Value") = "try"
ds.WriteXml("myconfig.xml")
'-----------

when run theres error :
'----------------
An unhandled exception of type 'System.NullReferenceException' occurred in ICLCorp.exe

Additional information: Object reference not set to an instance of an object.
'-----------

this is myconfig.xml :
'---------
<?xml version="1.0" encoding="utf-16"?>
<Books>
<Book ISBN="100000000" Title="IvbNET.com" Price="50.00" />
</Books>
'---------------

thanks

nicky
 
try

dim ds as new dataset

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
thanks crissie and RickGuy, i got the answer, i try this code and it working, thanks a alot
--------------
Dim ds As New DataSet, dstab As DataTable
ds.ReadXml("Myconfig.xml")

Dim t As DataTable, no As Int16 = -1
For Each t In ds.Tables
no += 1
MessageBox.Show(t.TableName & " no " & no)
If LCase(t.TableName) = LCase("Buku") Then
Dim dr() As DataRow = ds.Tables(no).Select("ISBN='100000000'")
dr(0).Item("Title") = "try"
ds.WriteXml("myconfig.xml")
End If
Next t

-----------
thanks

nicky
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top