0

我是新来的,对 VB.NET 也比较陌生。让我的 StreamWriter 正常工作有一个特定的问题。我的项目的要求之一是在用户单击“保存”按钮时为文件提供默认名称,我通过设置 mysave.Filename = "MyLog.log" 来完成此操作。我调用我的 streamwriter 的一个新实例,尝试保存到用户指定的文件名(再次为 mySave.Filename),但每次都保存到默认的 MyLog.log 文件。我在下面粘贴了我的代码。

如果有人能告诉我如何确保将数据保存为用户为文件名输入的值,那将非常有益。另外,我为代码格式道歉,它不是完全可读的,但我正在尝试学习如何使用 4 个空格缩进来发挥我的优势!!

谢谢!

Imports System.IO
Public Class Form1
Dim Initial As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug" 'give variable for full path without log file
Dim Fullpath As String = "C:\Users\Brian Frick\Documents\Visual Studio 2010\Projects\HW5_Frick_Creator\HW5_Frick_Creator\bin\Debug\MyLog.log" 'give one path for full path
Dim filewriter As New StreamWriter(Fullpath, True)

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Application.Exit() ' quit application
    filewriter.Close()
End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
    Dim mySave As New SaveFileDialog
    mySave.Filter = "LOG File (*.log)|*.log" 'set filter to .log
    mySave.DefaultExt = "log" 'set default extension
    mySave.InitialDirectory = Initial 'set default directory
    mySave.FileName = "MyLog.log"
    mySave.OverwritePrompt = True ' make sure to ask user if they want to over write
    If mySave.ShowDialog() = DialogResult.OK Then
        Dim filewriter As New StreamWriter(mySave.FileName, True)

        filewriter.Close()

        'filewriter = New StreamWriter(mySave.FileName, True)
        'filewriter.Close() 'close filewriter to allow access to write directory
        'Dim Stream As New StreamWriter(mySave.FileName, True) 'save file to path chosen by user in SaveDialog
        'Stream.Close() ' Close stream to allow access to directory
        ' filewriter.Close()
    Else
        'dialog cancelled - no action
    End If
    filewriter.Close()
    filewriter = New StreamWriter(Fullpath, True) 're initiate filewriter to be used in successive iterations through the program without error
End Sub

Private Sub SavingsDepositBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsDepositBtn.Click
    'write to file for SavDep
    filewriter.WriteLine("SavDep")
    filewriter.WriteLine(AccountBox.Text)
    filewriter.WriteLine(AmountBox.Text)
End Sub

Private Sub SavingsWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavingsWithdrawBtn.Click
    'write to file for SavWith
    filewriter.WriteLine("SavWith")
    filewriter.WriteLine(AccountBox.Text)
    filewriter.WriteLine(AmountBox.Text)
End Sub

Private Sub CheckDepsotBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckDepsotBtn.Click
    'write to file for CheckDep
    filewriter.WriteLine("CheckDep")
    filewriter.WriteLine(AccountBox.Text)
    filewriter.WriteLine(AmountBox.Text)
End Sub

Private Sub CheckWithdrawBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckWithdrawBtn.Click
    'write to file for CheckWith
    filewriter.WriteLine("CheckWith")
    filewriter.WriteLine(AccountBox.Text)
    filewriter.WriteLine(AmountBox.Text)
End Sub
End Class
4

1 回答 1

0

您使用文件编写器的一般方式将不起作用,并且还有一些其他严重的问题,例如有两个具有重叠范围的“FileWriter”变量。

对于您的保存例程,您不能只是重新实例化您的文件编写器以将其指向不同的路径。这将破坏底层流中的所有数据,因此您什么也没有保存。

尝试在程序运行时将数据缓存在内存中,并且在您准备好将所有数据写入其中之前不要创建流,并将其写入文件,一次完成。您当前的方法很难防止泄漏日志文件的句柄,因此如果应用程序崩溃,用户将不得不重新启动以释放写锁,然后才能再次运行它。查看“使用”构造和“尝试......最后”

于 2013-03-10T23:21:56.113 回答