How to send a notification to the user that the file is in use by another program
How do you send a notification to the user that the program cannot Autosave a file, while it is in use on another program? It cannot be saved even if there is a timer on the program on the RichTextBox_TextChanged event(). Here is the function:
Code:
Private Sub AutosaveFile()
Dim path As String = (My.Computer.FileSystem.SpecialDirectories.Desktop & "\Common-Mistakes-Backup.rtf")
' This text is added only once to the file.
If File.Exists(path) = False Then
Using fs As FileStream = File.Create(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Common-Mistakes-Backup.rtf")
fs.Close()
' Save the contents of the RichTextBox into the file.
RichTextBox1.SaveFile(path, RichTextBoxStreamType.RichText)
Timer1.Start()
End Using
End If
Try
' Delete the file if it exists.
If File.Exists(path) = True Then
' Save the contents of the RichTextBox into the file.
RichTextBox1.SaveFile(path, RichTextBoxStreamType.RichText)
Timer1.Start()
End If
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
Re: How to send a notification to the user that the file is in use by another program
Catch the exception and display a MessageBox.
Re: How to send a notification to the user that the file is in use by another program
Please clarify these points:
After the comment 'Delete the file... you never delete a file. Is the code incomplete?
What is the purpose of the timer?
As Sitten said, Catch the exception, but make sure to catch the specific exception so you do not lose control of other possible problems.
Re: How to send a notification to the user that the file is in use by another program
Because it bothered me so... I boiled down your code down to this:
Code:
Private Sub AutosaveFile()
Dim path As String = (My.Computer.FileSystem.SpecialDirectories.Desktop & "\Common-Mistakes-Backup.rtf")
Try
' Save the contents of the RichTextBox into the file.
RichTextBox1.SaveFile(path, RichTextBoxStreamType.RichText)
Catch exInUse As IO.IOException
MessageBox.Show("Unable to save the file, it is locked by another process.") 'This is where it shouild go for a locked file
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
AutosaveFile()
Timer1.Start()
End Sub
I'm not sure why you had it so complicated or what the point of the stream was for... all you needed to do is attempt to save the file, if it fails, you handle it... otherwise you go on. The timer should be stopped & restarted inside the tick event, not the autosave...
-tg
Re: How to send a notification to the user that the file is in use by another program
techgnome
Your answer was very good, you write very clean code. Thanks, I have given you a strong reputation.
Re: How to send a notification to the user that the file is in use by another program
Sitten Spynne
Good answer too, I had already figured a good way to do that. I have given you a good reputation too!