-
Jul 15th, 2016, 09:15 AM
#1
Thread Starter
Lively Member
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
Last edited by nqioweryuadfge; Jul 15th, 2016 at 09:19 AM.
-
Jul 15th, 2016, 09:56 AM
#2
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.
-
Jul 15th, 2016, 10:01 AM
#3
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.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Jul 15th, 2016, 11:38 AM
#4
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
-
Jul 16th, 2016, 04:41 AM
#5
Thread Starter
Lively Member
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.
-
Jul 16th, 2016, 04:42 AM
#6
Thread Starter
Lively Member
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!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|