Help with StreamReader
-
Been coding for about 3 months now and am currently making a "login" client for myself. I hve created a streamreader and have linked it to a .txt file which is located on my hardrive and i am struggling to get it to read of multiple lines so i can have more than one user. This is the code i have at the moment
Public Class Form1 Dim webe As New ProcessStartInfo() Dim username1 As New IO.StreamReader("C:\Textfile.txt") Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click If Username.Text & Password.Text = username1.ReadToEnd & "lol" Or Username.Text & Password.Text = "Admin" & "admin" Then StatusStrip.Text = "Welcome " & Username.Text
Thanks In Advance -
Been coding for about 3 months now and am currently making a "login" client for myself. I hve created a streamreader and have linked it to a .txt file which is located on my hardrive and i am struggling to get it to read of multiple lines so i can have more than one user. This is the code i have at the moment
Public Class Form1 Dim webe As New ProcessStartInfo() Dim username1 As New IO.StreamReader("C:\Textfile.txt") Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click If Username.Text & Password.Text = username1.ReadToEnd & "lol" Or Username.Text & Password.Text = "Admin" & "admin" Then StatusStrip.Text = "Welcome " & Username.Text
Thanks In AdvanceText file suppose to have more than one record there fore you should read the values inside a loop. Please see example below. The shown code sample below suppse to be inside Login_Click procedure :wtf:
Dim webe As New ProcessStartInfo() Dim username1 As New IO.StreamReader("C:\Textfile.txt") Do While NOT UserName.EndOfStream If UserName1.ReadLine() = UserName.Text.toString & PassWord.Text.ToString Then 'login information exist perform some type of actions exit do ElseIf UserName.Text & PassWord.Text = "Admin" & "admin" Then StatusStrip.text = "WelCome" & userName.Text End IF Loop UserName1.Close()
-- modified at 12:28 Monday 19th March, 2007
What a curious mind needs to discover knowledge is noting else than a pin-hole.
-
Text file suppose to have more than one record there fore you should read the values inside a loop. Please see example below. The shown code sample below suppse to be inside Login_Click procedure :wtf:
Dim webe As New ProcessStartInfo() Dim username1 As New IO.StreamReader("C:\Textfile.txt") Do While NOT UserName.EndOfStream If UserName1.ReadLine() = UserName.Text.toString & PassWord.Text.ToString Then 'login information exist perform some type of actions exit do ElseIf UserName.Text & PassWord.Text = "Admin" & "admin" Then StatusStrip.text = "WelCome" & userName.Text End IF Loop UserName1.Close()
-- modified at 12:28 Monday 19th March, 2007
What a curious mind needs to discover knowledge is noting else than a pin-hole.
I added in your code and i seen to be getting this error under the "Username.EndOfReader" 'EndofReader is not a members of the windows.form.textbox' Another expection i am getting is the "exit loop" is thrwoing an exception, 'exit must be followed by sub, function, property etc etc etc' I have tried to play about with it but still struggling with working it out.
-
I added in your code and i seen to be getting this error under the "Username.EndOfReader" 'EndofReader is not a members of the windows.form.textbox' Another expection i am getting is the "exit loop" is thrwoing an exception, 'exit must be followed by sub, function, property etc etc etc' I have tried to play about with it but still struggling with working it out.
-
Sorry my mistake... Convert .text.toString and then use exit do also Replace EndOfReader with EndOfStream... I apologize... :doh:
What a curious mind needs to discover knowledge is noting else than a pin-hole.
-
Text file suppose to have more than one record there fore you should read the values inside a loop. Please see example below. The shown code sample below suppse to be inside Login_Click procedure :wtf:
Dim webe As New ProcessStartInfo() Dim username1 As New IO.StreamReader("C:\Textfile.txt") Do While NOT UserName.EndOfStream If UserName1.ReadLine() = UserName.Text.toString & PassWord.Text.ToString Then 'login information exist perform some type of actions exit do ElseIf UserName.Text & PassWord.Text = "Admin" & "admin" Then StatusStrip.text = "WelCome" & userName.Text End IF Loop UserName1.Close()
-- modified at 12:28 Monday 19th March, 2007
What a curious mind needs to discover knowledge is noting else than a pin-hole.
JUNETY or anyone else, i have just had to format my pc and when i load up VS05 and my code i keep getting this error when debuging it, i have looked everywhere but it is really baffling me! any help will be appreciated!
Dim username1 As New IO.StreamReader("C:\username.txt") Dim password1 As New IO.StreamReader("C:\password.txt") Dim adminun As New IO.StreamReader("C:\adminun.txt") Dim adminpw As New IO.StreamReader("C:\adminpw.txt") Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click Do While username1.EndOfStream If username1.ReadLine() = Username.Text.ToString & Password.Text.ToString Then Exit Do End If Loop username1.Close() Do While password1.EndOfStream If username1.ReadLine() = Username.Text.ToString & Password.Text.ToString Then Exit Do End If Loop password1.Close() End Sub
ERROR:System.ObjectDisposedException was unhandled Message="Cannot read from a closed TextReader." ObjectName="" Source="mscorlib" StackTrace: at System.IO.__Error.ReaderClosed() at System.IO.StreamReader.get_EndOfStream() at WindowsApplication1.Form1.Login_Click(Object sender, EventArgs e) in C:\Users\Brad\Documents\Visual Studio 2005\Projects\SC Login\SC Login\Form1.vb:line 9 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(I
-
JUNETY or anyone else, i have just had to format my pc and when i load up VS05 and my code i keep getting this error when debuging it, i have looked everywhere but it is really baffling me! any help will be appreciated!
Dim username1 As New IO.StreamReader("C:\username.txt") Dim password1 As New IO.StreamReader("C:\password.txt") Dim adminun As New IO.StreamReader("C:\adminun.txt") Dim adminpw As New IO.StreamReader("C:\adminpw.txt") Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click Do While username1.EndOfStream If username1.ReadLine() = Username.Text.ToString & Password.Text.ToString Then Exit Do End If Loop username1.Close() Do While password1.EndOfStream If username1.ReadLine() = Username.Text.ToString & Password.Text.ToString Then Exit Do End If Loop password1.Close() End Sub
ERROR:System.ObjectDisposedException was unhandled Message="Cannot read from a closed TextReader." ObjectName="" Source="mscorlib" StackTrace: at System.IO.__Error.ReaderClosed() at System.IO.StreamReader.get_EndOfStream() at WindowsApplication1.Form1.Login_Click(Object sender, EventArgs e) in C:\Users\Brad\Documents\Visual Studio 2005\Projects\SC Login\SC Login\Form1.vb:line 9 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(I
Because you are making a mistake as follows: When you close the first stream therefore without reopening it you can't define UserName1 inside the second stream because its closed. Your approach to check username, password, etc. seems not appripriate. I suggest you to reorganize your code. :wtf:
What a curious mind needs to discover knowledge is noting else than a pin-hole.