使用这些代码,我不知道哪里出错了,我仍然不能解决我的问题
请检查并纠正我。怎么了?
我想要的是当他们点击保存文件按钮,savefiledialog将显示保存数据到一个文件。如果他们试图替换现有文件,Msgbox将显示拒绝他们的替换请求。所以他们必须创建一个新文件
这段代码是这样的
第一次点击,你可以创建或替换没有拒绝消息框。
第二次点击,您将收到拒绝您创建或替换。
第三次点击就像第一次点击
第四次点击就像第二次点击
等等
Private lastSaveFileName As String = String.Empty
Private Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click
lastSaveFileName = GetSaveFileName2(lastSaveFileName)
If Not String.IsNullOrEmpty(lastSaveFileName) Then
File.AppendAllText(lastSaveFileName, txtdisplay1.Text)
End If
End Sub
Private Function GetSaveFileName2(ByVal suggestedName As String) As String
Using sfd3 As New SaveFileDialog()
sfd3.Filter = "Text Files (*.txt) |*.txt"
sfd3.FileName = suggestedName
sfd3.OverwritePrompt = False
If sfd3.ShowDialog() = DialogResult.OK Then
If Not File.Exists(lastSaveFileName) Then
MessageBox.Show(
Me, "Your activity is not saved! This file have records from your last session, you cannot overwrite this file. Please create new file to save new records.",
"Save error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
)
Else
Return sfd3.FileName
End If
Else
End If
Return String.Empty
End Using
End Function
这是关于我的第一篇文章,如何WriteAllText,但限制覆盖现有的文件?
像这样,
Private Function GetSaveFileName2(suggestedName As String) As String
Dim rv As String = String.Empty 'String.Empty is do not save
Dim sfd3 As New SaveFileDialog()
sfd3.Filter = "Text Files (*.txt) |*.txt"
sfd3.FileName = suggestedName
sfd3.OverwritePrompt = False
Dim dr As DialogResult
Do
dr = sfd3.ShowDialog
If dr = DialogResult.OK Then
If IO.File.Exists(sfd3.FileName) Then
MessageBox.Show("Not saved! .... Please create new file to save new records or Cancel to Exit.",
"Save error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
rv = sfd3.FileName
End If
End If
Loop While dr = Windows.Forms.DialogResult.OK AndAlso rv = String.Empty
Return rv
End Function