Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,542 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,829 people online right now. Registration is fast and FREE... Join Now!




Reading/Writing to a file

2 Pages V  1 2 >  
Reply to this topicStart new topic

Reading/Writing to a file

NIXZ
22 Aug, 2008 - 02:15 PM
Post #1

D.I.C Head
**

Joined: 26 Jul, 2008
Posts: 87

Well lets say that you want to make a program and save its parametres (like color,user's name, anything) in 1 file
you can save it in different files like color.dat, username.dat, anything.dat but it is not practical how can i work with
an 1 and only file i think that the type of file is an .ini, right?
like this:
CODE

[color]
color = "white"

[username]
username = "someone"

[anything]
anything = "value"


but it will be difficult to moddify value then e.g: changing the color = "white" to color = "black"
plz post any suggestion even the dumpest one i will apreciate it...

This post has been edited by jayman9: 22 Aug, 2008 - 06:15 PM
User is offlineProfile CardPM
+Quote Post

wartech
RE: Reading/Writing To A File
22 Aug, 2008 - 03:53 PM
Post #2

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 106



Thanked: 3 times
My Contributions
Yes I believe you are trying to create an .ini file. How about something like this where ";" = commented out. You can setup a few options then manipulate the ini so that the semicolon is removed from the option you want activated and all other options are commented out. This worked in my particular situation when I needed to interface with an .ini file.

CODE


[color]
color = "white"
;color = "black"
;color = "red"

[username]
username = "someone"
;username = "someone else"


[anything]
anything = "value"
;anything = "another value"



User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Reading/Writing To A File
22 Aug, 2008 - 04:09 PM
Post #3

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,166



Thanked: 32 times
Dream Kudos: 1625
My Contributions
Have you considered using .NET applications settings? Take a look at my tutorial. It's about a bunch of stuff, but if you scroll down to the "Settings Tab" section, you should find what your looking for.
User is offlineProfile CardPM
+Quote Post

narmer93
RE: Reading/Writing To A File
22 Aug, 2008 - 04:24 PM
Post #4

D.I.C Head
**

Joined: 13 Mar, 2008
Posts: 241



Thanked: 1 times
My Contributions
well ,if it saves as an ini file,then,how to create an ini file
oh,is it in the tutorial?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Reading/Writing To A File
22 Aug, 2008 - 06:14 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Topic renamed to be more descriptive of the problem.

It can be an INI or it could be a TXT or a DAT, it really doesn't matter. Use the StreamWriter/StreamReader objects to read and write to the file. Just save it with whichever extension you wish.

Here is an example of how to do what you are asking.

Write to file:
CODE

Try
            Dim writer As New StreamWriter("myFile.ini")
            writer.WriteLine("My Color")
            writer.WriteLine("Username")
            writer.WriteLine("Anything")
            writer.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


An example of how to read that same file.
CODE

Dim thisColor As String = ""
        Dim username As String = ""
        Dim anything As String = ""

        Try
            Dim reader As New StreamReader("myFile.ini")
            thisColor = reader.ReadLine()
            username = reader.ReadLine()
            anything = reader.ReadLine()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


I output it to a messagebox so you could see the results of reading the file using the following line.
CODE

MessageBox.Show(thisColor & ControlChars.NewLine & username & ControlChars.NewLine & anything)


It is pretty simple and straight forward. Hope this helps.


Attached image(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

NIXZ
RE: Reading/Writing To A File
23 Aug, 2008 - 02:20 AM
Post #6

D.I.C Head
**

Joined: 26 Jul, 2008
Posts: 87

didnt though that readline exist thank you all!!!

This post has been edited by NIXZ: 23 Aug, 2008 - 02:21 AM
User is offlineProfile CardPM
+Quote Post

NIXZ
RE: Reading/Writing To A File
30 Aug, 2008 - 10:18 AM
Post #7

D.I.C Head
**

Joined: 26 Jul, 2008
Posts: 87

QUOTE(jayman9 @ 22 Aug, 2008 - 07:14 PM) *

Topic renamed to be more descriptive of the problem.

It can be an INI or it could be a TXT or a DAT, it really doesn't matter. Use the StreamWriter/StreamReader objects to read and write to the file. Just save it with whichever extension you wish.

Here is an example of how to do what you are asking.

Write to file:
CODE

Try
            Dim writer As New StreamWriter("myFile.ini")
            writer.WriteLine("My Color")
            writer.WriteLine("Username")
            writer.WriteLine("Anything")
            writer.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


An example of how to read that same file.
CODE

Dim thisColor As String = ""
        Dim username As String = ""
        Dim anything As String = ""

        Try
            Dim reader As New StreamReader("myFile.ini")
            thisColor = reader.ReadLine()
            username = reader.ReadLine()
            anything = reader.ReadLine()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


I output it to a messagebox so you could see the results of reading the file using the following line.
CODE

MessageBox.Show(thisColor & ControlChars.NewLine & username & ControlChars.NewLine & anything)


It is pretty simple and straight forward. Hope this helps.


ok just a question: why "StreamWriter" isn't declared ???
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Reading/Writing To A File
30 Aug, 2008 - 10:32 AM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You need to add this line to the top of your class (before the class declaration)

vb

Imports System.IO

User is online!Profile CardPM
+Quote Post

NIXZ
RE: Reading/Writing To A File
30 Aug, 2008 - 10:45 AM
Post #9

D.I.C Head
**

Joined: 26 Jul, 2008
Posts: 87

!Man i love this website no "Go find it by your own" comments...
wub.gif blush.gif

This post has been edited by NIXZ: 30 Aug, 2008 - 10:46 AM
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Reading/Writing To A File
30 Aug, 2008 - 10:49 AM
Post #10

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
DreamInCode is one of the top programming sites. So they would not even think of writing that. That's what is so great about DreamInCode.
User is online!Profile CardPM
+Quote Post

jacobjordan
RE: Reading/Writing To A File
30 Aug, 2008 - 11:21 AM
Post #11

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,166



Thanked: 32 times
Dream Kudos: 1625
My Contributions
Didn't work out well for this guy:
http://www.dreamincode.net/forums/showtopic60531.htm
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Reading/Writing To A File
30 Aug, 2008 - 11:25 AM
Post #12

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
That guy doesn't count because he expected us to do the work for him.
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:40PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month