Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Imports System.IO
Public Class Form1

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Label2.Text = "Enter amount to withdraw:"
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Label3.Text += "0"
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Label3.Text += "3"
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Label3.Text += "2"
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Label3.Text += "1"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Label3.Text += "4"
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Label3.Text += "5"
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Label3.Text += "6"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Label3.Text += "7"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label3.Text += "8"
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Label3.Text += "9"
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Close()
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        MessageBox.Show("Bashar al Assad is struggling to take over Lebanon! NEWS FROM LBC!!!")
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Label2.Text = "Enter amount to deposit:"
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Dim txFile As New StreamReader(Environment.CurrentDirectory + "\\myfile.txt")
        Dim myfile As String
        Dim amount As Double
        myfile = txFile.ReadToEnd

        If (Label2.Text = "Enter PIN") Then
            If (myfile.Contains(Label3.Text)) Then
                Button12.Enabled = True
                Button13.Enabled = True
                Label2.Text = "PIN Accepted"
                MessageBox.Show("welcome user! you are logged in")
                Label2.Text = "How may we help you?"
                Label3.Text = ""
            Else
                MessageBox.Show("pass is incorrect try again")
                Label3.Text = ""
            End If
        ElseIf (Label2.Text = "Enter amount to deposit:") Then
            RichTextBox1.Text = myfile
            RichTextBox1.Text.Replace("3234", Label3.Text)
            Dim newamount = Convert.ToDouble(Label3.Text)
            amount = amount + newamount
            MessageBox.Show(amount)
            System.IO.File.WriteAllText("myfile.txt", RichTextBox1.Text)
            Dim sw As New System.IO.StreamWriter("myfile.txt")

        ElseIf (Label2.Text = "Enter amount to withdraw:") Then
            RichTextBox1.Text = myfile
            RichTextBox1.Text.Replace("3234", Label3.Text)
            Dim newamount = Convert.ToDouble(Label3.Text)
            amount = amount - newamount
            MessageBox.Show(amount)
            System.IO.File.WriteAllText("myfile.txt", RichTextBox1.Text)
            Dim sw As New System.IO.StreamWriter("myfile.txt")
        End If


        ' File.WriteAllText("myfile.txt", Label3.Text)
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class
Posted
Updated 17-Nov-14 7:24am
v2

1 solution

Why are you doing this:
VB
System.IO.File.WriteAllText("myfile.txt", RichTextBox1.Text)
        Dim sw As New System.IO.StreamWriter("myfile.txt")
    End If


    ' File.WriteAllText("myfile.txt", Label3.Text)
End Sub

There are a huge number of things wrong with this, but the important ones are:
1) It assumes the text file is in the application executable directory: this works in development, but fails in production because the app is installed to the "Program Files" folder, and that is read only unless your app is elevated - which means UAE. See here Where should I store my data?[^] for better places - it's C# code instead of VB, but it should translate very easily.
2) If you look at the StreamWriter documentation: MSDN[^], it says:
If the file exists, it is overwritten; otherwise, a new file is created.So the date you just wrote to the file with WriteAllText is deleted by the following instruction...
3) (and this is the one you noticed) StreamWriter opens the file for writing - which means it takes an exclusive lock on the file - so you can't do anythign else to teh file until the StreamWriter is closed - and since you don't close it, or Dispose the instance, itr will stay open until the Garbage Collector gets round to deleting it - which could be tomorrow, next week, next month...or when the app closes, whichever comes first.
 
Share this answer
 
Comments
Richard Deeming 17-Nov-14 15:26pm    
"... unless your app is elevated - which means UAE."

Did you mean "UAC", or are you running your code in Dubai? :P
OriginalGriff 17-Nov-14 15:36pm    
UAC :blush:
It's been a long day...
jkirkerx 17-Nov-14 17:55pm    
I think he meant to elevate as high as the Burj Khalifa for UAE
http://en.wikipedia.org/wiki/Burj_Khalifa

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900