本文介绍了ATM机:将文件存储到文本文件:错误:进程无法访问该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

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



这有很多问题,但重要的是是:

1)它假定文本文件位于应用程序可执行文件目录中:这在开发中有效,但在生产中失败,因为应用程序已安装到Program Files文件夹,并且已读取除非你的应用程序被提升 - 这意味着阿联酋。请参阅此处 []更好的地方 - 它是C#代码而不是VB,但它应该很容易翻译。

2)如果你看一下StreamWriter文档: [],它说:

如果文件存在,则会被覆盖;否则,会创建一个新文件。因此,您使用WriteAllText写入文件的日期将被以下指令删除...

3)(这是您注意到的那个)StreamWriter打开文件进行写入 - 这意味着它需要对文件进行独占锁定 - 因此在StreamWriter关闭之前你不能对文件做任何其他操作 - 并且因为你没有关闭它,或者处理实例, itr将保持打开状态,直到垃圾收集器轮流删除它 - 可能是明天,下周,下个月......或者当应用程序关闭时,以先到者为准。


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.


这篇关于ATM机:将文件存储到文本文件:错误:进程无法访问该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:04