Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Settings? (http://www.programmingforums.org/showthread.php?t=5862)

brokenhope Sep 11th, 2005 5:28 PM

Settings?
 
Alright on my program I want to somehow make it remember the users settings. Such as default directory to open files from, font face, font size, font type, auto tab, syntax highlighting, practicle things like that, how should I store that? The only thing I can think of is flatfile, but is there a better way with less risk of the user opening the file and messing with it?

The second type of things I need stored is, abbreviations, which is basicly a list of keywords you can type that will be replaced by its replacement, that list can be quite long so im guessing flatfile will work best for that, I dont know, any advice?

Please post your opinions on this... Ive searched google but I didnt find anything at all relevant to this topic.

Ooble Sep 11th, 2005 5:46 PM

You can use a flat file or the Windows registry. Personally, I prefer a flat or XML-based file. If a user's messing around, just hope they know what they're doing and add error-handling. :p

brokenhope Sep 11th, 2005 6:43 PM

Hmm dont know anything about windows registry. XML does seem like it would be appropriate, but I know nothing about it, so I guess flatfile will have to do. Do you recommend me using .txt as the flatfiles format? or something else?

Ooble Sep 11th, 2005 8:17 PM

Use a straight text file, and just change the extension to something confusing. ".dat" (data) is a good one.

brokenhope Sep 12th, 2005 9:21 PM

Alright, Ill do that, but anyways I got stumped on this... I know what is wrong, with my code but the fix seems a little bit to complex for VB .NET, its supposed to make things simple, and ive never seen it do anything with one character per array value like in C and C++, where you cant store strings in arrays... which the code hints in the code im using now is telling me one byte per... this is code I altered quite a bit from the example I found on the internet, so I might have screwed something up. But basicly what my code is supposed to do is:

open up the file elementalEditor.dat, if it doesnt exist create it, then check the size of the file, if its 0 (meaning it was just created, atleast I think) then write default values to it).

:


        ' Load Settings
        Dim oFile As System.IO.File
        Dim oFileStream As System.IO.FileStream
        oFileStream = oFile.Open("elementalEditor.dat", IO.FileMode.OpenOrCreate)
        If oFileStream.Length = 0 Then
            ' Write default settings to form
            oFileStream.Write(, 0, 100000000)
        End If


The values I write to it will be in a simple form, atleast to me, one configuration value per line, so I can read through it one line at a time, take all the text on that line and put it into the shared configuration array.

Anyways know an eaiser way to write code to the file like that without having to do it like VB is telling me, one byte per value in array?

Ooble Sep 13th, 2005 1:41 PM

Quote:

Originally Posted by brokenhope
then check the size of the file, if its 0 (meaning it was just created, atleast I think)

Nope. It just tells you how big the file is in bytes. Of course it's going to be zero if you just created it! This is how I do it in C# - I assume it's similar for VB .NET:
:

try
{
    oFileStream = oFile.Open("elementalEditor.dat", IO.FileMode.OpenOrCreate);
}
catch
{
    return;
}

oFileStream.Write(...);


Infinite Recursion Sep 13th, 2005 2:24 PM

Here's a logging method I wrote in VB .Net a while back, maybe it will help:

:

    Public Function LOG(ByRef info As String) As Object
        On Error Resume Next
        If Err.Number = 0 Then
            FileOpen(1, LOGFILE, OpenMode.Append)
            PrintLine(1, TimeOfDay & "-> " & info)
            FileClose(1)
        End If
    End Function


brokenhope Sep 14th, 2005 8:06 PM

Thanks for all your help, I have it semi working now. Theres just one kink in the updateing of certain settings.

First I will start off and say how im updateing specific lines, just in case theres a better way, Im looping through every line and adding it to a string, while doing it theres a counter counting each time through the loop when it reaches the line number, instead of inserting the files text on that line, it uses the new text, then when its done with the loop it writes the new text. The problem is, I cant get it to overwrite over whats already in the file, it just adds onto it. I want to completely get rid of every character in the file before writing the new multi line string to it...

heres my code:

:

            Dim oFileStream As New System.IO.FileStream("elementalEditor.dat", IO.FileMode.Open)
            Dim oStreamWriter As New System.IO.StreamWriter(oFileStream)
            Dim oStreamReader As New System.IO.StreamReader(oFileStream)
            Dim iCount As Integer = 0
            Dim strEditFileText As String
            oStreamReader.BaseStream.Seek(0, IO.SeekOrigin.Begin)

            While oStreamReader.Peek <> -1
                ' Line 1 is the default open directory.
                If iCount = 0 Then
                    strEditFileText = strEditFileText & txtOpenFileDirectory.Text & vbNewLine
                    oStreamReader.ReadLine()
                Else
                    strEditFileText = strEditFileText & oStreamReader.ReadLine() & vbNewLine
                End If
                iCount = iCount + 1
            End While

            ' Change Position of stream writer to start of file
            oStreamWriter.BaseStream.Seek(0, IO.SeekOrigin.Begin)
            oStreamWriter.Write(strEditFileText)

            oStreamWriter.Flush()
            oStreamWriter.Close()
            oStreamReader.Close()
            oFileStream.Close()


Is there something I can specify that does that or do I have to delete and recreate the file or something like that?

Dameon Sep 14th, 2005 8:11 PM

Perhaps you should try Nini

brokenhope Sep 14th, 2005 8:43 PM

Yea that would make things simpler, but I dont know much about .NET and dont know how or what to do with it to make it work. I really want to create this program all by myself so I can figure out how to write things, like write code to a file correctly, before I start taking shortcuts, just so I have the knowledge.


All times are GMT -5. The time now is 4:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC