open file, get variables

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
okay, i'm creating a very simple log- in program. This is what it does, you type in a username and password, hit the register button. you can then enter a username and password in the login box, if it matches a textbox will say "welcome " + uservariable. You can register multiple users. However, I want the variables to be remembered once the program is closed, so your registered users stay.
I have been told by a friend i need to look into opening and writing word documents. Can someone tell me how to do this, and then how to retrieve the written data in the word document as a variable?
 
First off I wouldn't consider storing data of any sort in a Word document, unless of course, it was a document...which user/pass pairs are certantly not ;-)

You might consider storing them as entries in a text/XML file if security isnt a big concern...you'd definitly want to consider some sort of hashing/encryption if it is.

My approach here, as this sounds like a learning/homebrew app would be to get a little of each world and store the data as a class that you then serialize to a file. At the program load, you'd deserialize the data back into class structure for use.

Read up on this: Binary Formatter

Then provided you already know how to work with file streams, you do the following in your code:

Build a class stucture to store your user/pass combinations in something like an arraylist. On exit, serialize (using the Binary Formatter) the class to disk. Then on load, have code that deserializes back to class stucture. In this way, you can add and delete from the class, then serialize back to disk on exit and save your users/passes.

Research into some of that and feel free to come back with follow up questions, as I'm sure more detail will be necessary. (Again, this is only one approach..just the first I thought of from what I invision your goal/needs to be.)


Try researching these two things:
 
open file, get variable

Hello. I have a simple method for it, providing you don't know already?
Put a username in a RichTextBox and save it. ie
VB.NET:
RichTextBox1.SaveFile("username.rtf")
This can be done by click event or form leaving.
THEN: When coming back to program put this in Load_event:-
VB.NET:
RichTextBox1.LoadFile("username.rtf")
And the same name appears!!
You can have quite a lot providing you give each a different .rtf file name. And the name of the RichTextBox can be different from saving to loading. To iterate, the name in the parentheses has to be unique for each person.
Hope that helps.
 
yeah that's simple. It sounds like Ravens is better, but too complcated for a noob like me at this time.
 
Serialization isnt all that bad, I had to learn it a while back to send stuff across a network stream and found that resources here and other places were of great help. Im out for a bit, but Ill try to post you some sample code as soon as I get back tonight to get you started.
 
okay, how do i save the value of a variable without using a rich text box. the variable i want to save goes up by one each time a user is registered, and makes sure that the previous user is not overwritten when a new one is registered. Now i could have an invisible rich text box that is equal to the variable and save that at close and load it at start, but there must be a simpler way to save the value of a variable.
 
open file get variables

I'm not quite sure if I got you right?
Give a different .rtf file name for each person as you add:-
RichTextBox1.SaveFile("person1.rtf")
RichTextBox1.SaveFile("person2.rtf") and so on

You need only use one RichTextBox. The person's username is entered and in the click event the SaveFile is used, then the RTB is cleared ready for the next person to be entered. OK.
 
right. But now, when username and password is entered and login button is pressed, it needs to test the input against the registered information. This is how I've done it.When the registered button is clicked, the variable reg +=1.I then have an array where password(reg) = RegisteredUserTextBoxI have done the same thing with the username. This way, when you register again, it won't overwrite the one before. My problem is that I need it to remember what the variable reg was when it was last used, so it won't overwrite the users when you start it up again.So how should I save the reg variable? The only way I could think of was by making an invisible richtextbox equal reg, and saving it at form close. It would then load the richtextboxfile into the rich text box on form load, and make the variable reg = the text in the textbox.It is very complicated, and I'm sure there is an easier way. This is my first time working with saving files and using arrays, so bear with me. I feel like such a noob =(
 
As I said, the serialization to disk is only one approach, and you can take whichever your desire, but I'm not so sure about the RTB approach, it just seems awkward.

Basically, I would approach as follows. I've provided code where I think its necessary, you fill in the holes and you'll have a good learning experience.

You need to include a few resources:
VB.NET:
Imports System.io 'For the file stream
Imports System.Runtime.Serialization.Formatters.Binary 'For the binary formatter :-)
You will need a class to store your users. Notice its marked as Serializable() so that it can be written to a stream (file stream, in our example)
VB.NET:
<Serializable()> Public Class clsUsers
    Public strUsername As String
    Public strPassword As String
End Class
So, we'll be storing our users as instances of the clsUsers object in an arraylist. This list we need to declare as global to our app, so we put it right inside our "Class Form1" definition.

VB.NET:
Class Form1
    Dim alAllUsers As New ArrayList
Then, when we load the app (Form1_LoaD) we would want to load the data from disk (our users list) that we saved last time we left the app.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myFileStream As New FileStream(Application.StartupPath & "\Users.cfg", FileMode.Open)
        Dim myBinaryFormatter As New BinaryFormatter
        alAllUsers = DirectCast(myBinaryFormatter.Deserialize(myFileStream), ArrayList)
        myFileStream.Close()
        myFileStream.Dispose()
        myBinaryFormatter = Nothing
End Sub
We also want a user to be able to register, so we have a button for that:

VB.NET:
Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        'Loop through arraylist to check if the user already exists.

        Dim myUser As New clsUsers
        myUser.strPassword = Me.txtUsername.Text
        myUser.strUsername = Me.txtUsername.Text

        alAllUsers.Add(myUser)
        myUser = Nothing
end Sub
And, of course, we want to save the new arraylist on the way out of the program.

VB.NET:
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        'Leaving the program, need to save our new users file.
        Dim myFileStream As New FileStream(Application.StartupPath & "\Users.cfg", FileMode.Open)   'Open the file stream...
        Dim myBinaryFormatter As New BinaryFormatter            'Initialize the formatter...
        myBinaryFormatter.Serialize(myFileStream, alAllUsers)   'Using our formatter, write the alAllUsers to disk.
        myFileStream.Close()
        myFileStream.Dispose()
        myBinaryFormatter = Nothing
End Sub
I know its a lot of code, and likely a bit 'advanced' to what your doing right now, but if you can spend some time studying the method you'll find it an easy to use solution.

Remember, all its really doing is:

Load Program
Load Data From Disk
Allow user to register
Close Program
Write Data to Disk

From here, you could take the array list and check user/pass combinations when someone tries to login, etc. Please feel free to ask more questions, I'd love to have you understand the method here and am more than willing to explain or re-explain parts as necessary. Also, in all that text, please forgive any mistakes, I'll review in a minute to make sure I didn't include anything to confusing.
 
open files get variables

It is up to you what you do. I always go for the simplest way instead of elaborate solutions where one can get bogged down on routines that the more experienced programmers are conversant with. For my money's worth(Not-A-Lot!!) I'd build basic to start with and have a program that works. THEN: Increase your learning with info that Raven65 gives.
Whatever way you go - good luck.:)
 
First of all, your project has something called 'Settings'. You should probably use these 'settings' if you are just learning..

Go to Project->Settings; then go to 'Settings' tab, you will see stuff in a row / column list, click on the first row, type 'Users', then the second row 'Passwords'.

To access these settings, do

VB.NET:
Me.Settings.<SettingName>.*

To save them

VB.NET:
Me.Settings.Save()

Then just store users/passwords in an array then store the array in the respected row in the settings.
 
I can't seem to find the settings. I click on project, but there is nothing about settings in the drop menu? where do u find this?
 
Raven, it says that I can't dispsose myfilestream because it is protected. I was inputing your code examples into form load and form closed when I got the error
 
k, couple questions for raven. ON form load should i not use openorcreate instead of open? That way it will create the user.cfg file the first time run. Also, when running the form load code you gave, is says 'attempting to deserialize an empty string'. Any ideas on how to fix that?

oh and by the way, Jason, you way sounds the best by far, but I cannot find these settings anywhere. lol
 
Back
Top