Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini
Editing a certain string in a .ini Editing a certain string in a .ini
Editing a certain string in a .ini
Go Back  Xtreme Visual Basic Talk > > > Editing a certain string in a .ini


Reply
 
Thread Tools Display Modes
  #1  
Old 08-27-2008, 03:13 PM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default Editing a certain string in a .ini


Hey, im pretty new to VB.net and a newbie >_< lol sorry, but i werent sure where this belonged pls dont flame me :X

Anyway what im trying to do is create a launcher for a program (a game) and i am in need of assistance on this one thing
Basically its an options window, i need each of the custom options to be saved to the INI file, for example

Default:

resolution 800 600
fullscreen 0
texture 1
view 0
detail 0
distant 0
shadow 0
bloom 0

etc etc
now if i add radio buttons on my options form as the fullscreen etc are booleans and a drop down list for the resolution how would i let the program go through the .ini file to look for the specific thing.

I would like an example for resolution and one of the others (Fullscreen should do) just so i can get the grasp of how to do it,

i understand you can use StreamReader and StreamWriter but i dont fully understand the concept, i would be HIGHLY greatfull if somone can give me a solution to this problem!

Also it will need to read from the ini and set the radio buttons and the drop down list to what is already set in the .ini file



SO just to get it straight incase i havnt made sense, i need this to go into a ini, read it, spit out the information using radio buttons and a drop down list, user changed the radio buttons and drop down list, presses save, changes the INI to their desired choices, DONE

/02goswej
Reply With Quote
  #2  
Old 08-27-2008, 03:15 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Instead of using an INI file, you can use create settings in the project properties and then use My.Settings to access them. This is much easier than coming up with something on your own.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #3  
Old 08-27-2008, 03:31 PM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default

I cant, the game itself reads from an .INI its an official game im making a launcher so me and my friends can change options etc without going on game and restarting all the time, its for a community, i cant change the fact that its reading from .ini :/

Last edited by 02goswej; 08-27-2008 at 03:39 PM.
Reply With Quote
  #4  
Old 08-27-2008, 03:40 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

You can use the Nini library to read and write to INI files.

http://nini.sourceforge.net/

No sense reinventing the wheel.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #5  
Old 08-27-2008, 03:42 PM
AtmaWeapon's Avatar
AtmaWeaponEditing a certain string in a .ini AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

*bleh no need to use API I deleted this part of my post*

I've never seen Nini, but I'd recommend using that instead of API since it's always nice to have a library doing the work for you.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 08-27-2008, 03:44 PM
DubbleClick's Avatar
DubbleClick DubbleClick is offline
Contributor
 
Join Date: Mar 2005
Location: Tennessee
Posts: 511
Default

Do the API's WritePrivateProfileString and GetPrivateProfileString not work in .Net?

Just asking....

Also, that doesn't look like standard INI formatting. It's more like
[Default]
resolution=800 600
fullscreen=0
texture=1
view=0
detail=0
distant=0
shadow=0
bloom=0

But then again, the game may have it's own parser... Too bad people do that and give filenames the same extension.

Parsers aren't that hard to write. Sounds like a good learning experience for you. As far as I know there are no API's that parse files out in that format.
Edit: guess I was wrong... Nini?


Edit: So is that another format of an INI file then? Never payed any attention and noticed any like that... nice.
Reply With Quote
  #7  
Old 08-27-2008, 03:55 PM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default

Like i said O_O im a newb lol i downloaded this nini library how do i import it into my Visual Basic 2008 Express?
Reply With Quote
  #8  
Old 08-27-2008, 04:30 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

You can add a reference to the DLL to your project.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #9  
Old 08-27-2008, 04:45 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Quote:
Originally Posted by AtmaWeapon View Post
I've never seen Nini, but I'd recommend using that instead of API since it's always nice to have a library doing the work for you.
It's a nice little library. I haven't used it for INI files, but have for XML files to store bits of data that I didn't want to put into the project settings or in a database.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #10  
Old 08-27-2008, 05:07 PM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default

Thanks for the help but i dont think the Nini is going to help, this INI is wierd and it is setup like the following:

Code:
//browser ini file

//option
resolution 1360 768
fullscreen 0
texture 1
view 0
detail 0
distant 0
shadow 0
bloom 0
WindowAlpha 255
SlangWord 0
ChatCommand 2
InstantHelp 0
Interface 1
RollEffect 0
saveAccount 1
account "*****"
notice 0
noticeTime 1218336842
tip 1
Trade 1
Say 1
Party 1
Shout 1
MessengerJoin 1
Messenger 1
Server 0 1
Sound 0 0.0 0.0
Guide 2
Guide2 2
Guide3 2
SkillShowMessage 1
ChatFilter0 27
ChatFilter1 3
ChatFilter2 5
ChatFilter3 9
ChatFilter4 17
ChatFilterEtc 832
ip "*************"
DamageRender 1
BuffTimeRender 1
MouseSpeed 1
SFXRenderOff 0
CameraLock 1
AutoAttack 1
PRenderName 1
OPRenderName 1
NRenderName 1
BRIGHT 10
GAMMA 1.000000
CONTRAST 1.000000
BUFFSTATUSMODE 0
ZOOMLIMIT 1
SFXLEVEL 5
Reply With Quote
  #11  
Old 08-27-2008, 05:21 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Not the usual INI format.

You can still use Nini. Download the source code and modify it to support that format.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #12  
Old 08-27-2008, 05:27 PM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default

Uhm O_O
<<<Newbie here >_< lol! is there an easier way?!
Reply With Quote
  #13  
Old 08-27-2008, 05:31 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Generally, editing something is easier than writing something from scratch. Even if it is a language you know little about, like C#. You might be able to spot the places that need to be changed. Then use VS to recompile the DLL.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #14  
Old 08-27-2008, 05:54 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

To get you started, the section below from IniReader.cs needs to be modified to look for a "/" instead of a "[", since you INI format labels a section starting with a "//"

Code:
switch (ch)
{
	case ' ':
	case '\t':
	case '\r':
		SkipWhitespace ();
		ReadNext ();
		break;
	case '\n':
		ReadChar ();
		break;
	case '[':
		ReadSection ();
		break;
	case -1:
		readState = IniReadState.EndOfFile;
		result = false;
		break;
	default:
		ReadKey ();
		break;
}
In the block below, you are not looking for an end "]", so don't throw the exception.

Code:
/// <summary>
/// Reads an INI section.
/// </summary>
private void ReadSection ()
{
	int ch = -1;
	iniType = IniType.Section;
	ch = ReadChar (); // consume "["
	while (true)
	{
		ch = PeekChar ();
		if (ch == ']') {
			break;
		}
		if (EndOfLine (ch)) {
			throw new IniException (this, "Expected section end (])");
		}

		this.name.Append ((char)ReadChar ());
	}

	ConsumeToEnd (); // all after '[' is garbage			
	RemoveTrailingWhitespace (this.name);
}
Reading a key and a value may be a little trickier, but with a little trial and error, you might be able to figure it out.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #15  
Old 08-27-2008, 06:23 PM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default

i changed the things you said now, how do i re-compile it into a DLL, i cant open the project i get errors, im using VB.NEt Express 2008
Reply With Quote
  #16  
Old 08-27-2008, 07:26 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Here's a project in VB.NET that you may have an easier time editing and compiling.

http://www.codeproject.com/KB/vb/VB....le_Object.aspx
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.
Reply With Quote
  #17  
Old 08-27-2008, 09:09 PM
AtmaWeapon's Avatar
AtmaWeaponEditing a certain string in a .ini AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Eh editing the source of a configuration library seems kind of like a lot of effort; wouldn't it be easier to tell him to read the file, make replacements, then write the file again?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #18  
Old 08-27-2008, 09:54 PM
gpraceman's Avatar
gpracemanEditing a certain string in a .ini gpraceman is offline
Contributor

* Expert *
 
Join Date: Sep 2002
Location: Highlands Ranch, CO
Posts: 560
Default

Quote:
Originally Posted by AtmaWeapon View Post
Eh editing the source of a configuration library seems kind of like a lot of effort; wouldn't it be easier to tell him to read the file, make replacements, then write the file again?
It seems like the INI file is for some app not of his own creation.

Edit: I took your comment as being that he should just reformat the INI file, but that may not be what you meant. It may be easier for him to read it, parse it, write changes back to it (in the same format that is has), but then again I think it may be easier to take an existing parser and tweak it to support that particular file's format. Either way, there's lessons that can be learned.
__________________
Awana Grand Prix and Pinewood Derby racing - Where a child, an adult and a small block of wood combine for a lot of fun and memories.

Last edited by gpraceman; 08-27-2008 at 10:05 PM.
Reply With Quote
  #19  
Old 08-28-2008, 05:49 AM
02goswej 02goswej is offline
Newcomer
 
Join Date: Aug 2008
Posts: 7
Default

I was thinking of the exact same thing, i just need some code to start me off, user makes changes then writes the whole thing in the correct format.

so everytime somone clicks on the options button it would just rewrite the whole thing to however they set it on that form, in the correct format, im just not sure HOW >_< man i wish i knew more about this thing but im still learning, sorry to be a pain :X
Reply With Quote
  #20  
Old 08-28-2008, 07:46 AM
AtmaWeapon's Avatar
AtmaWeaponEditing a certain string in a .ini AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

There's a good file I/O tutorial in the tutor's corner that will get you up to speed on file I/O in .NET.

The most efficient way to modify this file is going to be to read a line, modify it if needed, then write the line back out. Of course, this isn't possible since you can't write to the file while you're reading it, but you can approximate this by keeping the contents of the new file in memory. Here's a short program that hard-codes the resolution to 800x600:

Code:
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim newFile As New StringBuilder()

        Using reader As New StreamReader("file.ini")
            While Not reader.EndOfStream
                Dim currentLine As String = reader.ReadLine()

                If currentLine.StartsWith("resolution", StringComparison.OrdinalIgnoreCase) Then
                    currentLine = "resolution 800 600"
                End If

                newFile.AppendLine(currentLine)
            End While
        End Using

        Using writer As New StreamWriter("file.ini")
            writer.Write(newFile.ToString())
        End Using
    End Sub

End Module
It wouldn't even take a lot of time to create a program that does this in a general sense. One approach would be to make a class with a property for each line of the ini file, then have the class write itself. A less tedious approach would involve keeping a dictionary that links words to their values; strong typing for error checking would be nice, but if you treat all values as strings it's not too difficult. Here's a modification of the program above that lets you change an arbitrary number of values:
Code:
Imports System.IO
Imports System.Text

Module MainModule

    Private _substitutions As New Dictionary(Of String, String())

    Public Sub Main()
        ' Change the resolution to 1024x768 and fullscreen to 1
        _substitutions.Add("resolution", MakeStringArray("1024", "768"))
        _substitutions.Add("fullscreen", MakeStringArray("1"))

        ModifyFile()
    End Sub

    Private Function MakeOptionString(ByVal key As String) As String
        Dim optionString As New StringBuilder()

        If _substitutions.ContainsKey(key) Then
            optionString.Append(key)

            For Each item As String In _substitutions(key)
                optionString.AppendFormat(" {0}", item)
            Next
        End If

        Return optionString.ToString()
    End Function

    Private Function MakeStringArray(ByVal ParamArray args() As String) As String()
        Return args
    End Function

    Private Sub ModifyFile()
        Dim newFile As New StringBuilder()

        Using reader As New StreamReader("options.ini")
            While Not reader.EndOfStream
                Dim currentLine As String = reader.ReadLine()

                For Each opt As String In _substitutions.Keys
                    If currentLine.StartsWith(opt, StringComparison.OrdinalIgnoreCase) Then
                        currentLine = MakeOptionString(opt)
                    End If
                Next

                newFile.AppendLine(currentLine)
            End While
        End Using

        Using writer As New StreamWriter("options.ini")
            writer.Write(newFile.ToString())
        End Using
    End Sub
End Module
From there, it's just a matter of creating a UI that can convert user input into the right set of substitutions.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini Editing a certain string in a .ini
Editing a certain string in a .ini
Editing a certain string in a .ini
 
Editing a certain string in a .ini
Editing a certain string in a .ini
 
-->