Results 1 to 6 of 6

Thread: How to send a notification to the user that the file is in use by another program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    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.

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,837

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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
  •  



Click Here to Expand Forum to Full Width