An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'C:\Users\ACER\Desktop\Common-Mistakes-Backup.rtf' because it is being used by another process.
The error appears when I open a file that has been saved or updated, when running the program at the same time. I want users to be able to look at their files whenever they want without the need of closing the program first after it is installed. The problem appear when I try Save or when I try to use Autosave Function that acts as a backup incase of power failure. Here is the code with the problem:
Here is the rest of the code:Code:ElseIf File.Exists(path) = True Then ' Save the contents of the RichTextBox into the file. RichTextBox1.SaveFile(path, _ RichTextBoxStreamType.RichText) Timer1.Start() End If
Code:Private Sub SaveRecord() 'Declare a SaveFileDialog object Dim objSaveFileDialog As New SaveFileDialog 'Set the Save dialog properties With objSaveFileDialog .DefaultExt = ".rtf" .FileName = "Common-Mistakes" .Filter = "Word Document (*.rtf)|*.rtf|(*.txt)|*.txt|All files (*.*)|*.*" .FilterIndex = 1 .OverwritePrompt = True .Title = "SaveAs File Dialog" End With Dim path As String = (My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Common-Mistakes.rtf") ' This text is added only once to the file. If File.Exists(path) = False Then If objSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Try Dim filePath As String = System.IO.Path.Combine( _ My.Computer.FileSystem.SpecialDirectories.MyDocuments, _ objSaveFileDialog.FileName) ' Save the contents of the RichTextBox into the file. RichTextBox1.SaveFile(filePath, _ RichTextBoxStreamType.RichText) Catch fileException As Exception Throw fileException End Try End If ElseIf File.Exists(path) = True Then ' Save the contents of the RichTextBox into the file. RichTextBox1.SaveFile(path, _ RichTextBoxStreamType.RichText) MessageBox.Show("You have already saved on that location!", "File Already Saved!", MessageBoxButtons.OK) End If 'Clean up objSaveFileDialog.Dispose() objSaveFileDialog = Nothing End Sub 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 ' Save the contents of the RichTextBox into the file. RichTextBox1.SaveFile(path, _ RichTextBoxStreamType.RichText) ElseIf File.Exists(path) = True Then ' Save the contents of the RichTextBox into the file. RichTextBox1.SaveFile(path, _ RichTextBoxStreamType.RichText) Timer1.Start() End If End Sub 'End of Save Functions Sections.'




Reply With Quote
