File isn't available to visual basic....

NJDubois

Well-known member
Joined
May 15, 2008
Messages
84
Programming Experience
Beginner
I'm trying to convert audio files from AU to MP3 that are stored with dropbox. The audio file is downloaded to the dropbox client folder in windows, and then my application would be ran.

While trying to convert the file, I run into file in use issues. So I use this bit of code to determine if its free:

VB.NET:
    Public Function FileInUse(ByVal sFile As String) As Boolean
        Dim thisFileInUse As Boolean = False
        If System.IO.File.Exists(sFile) Then
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    thisFileInUse = False
                End Using
            Catch
                thisFileInUse = True
            End Try
        End If
        Return thisFileInUse
    End Function

But that wasn't good enough. I would still get issues. So I though, well maybe the file is still downloading so I added a file size check, and I wait for the filesize to stop changing.

When that is detected, I still get file in use related errors. That chunk of code doesn't detect it. I've bashed my head into the wall over this for a long time.

It comes down to this. While the dropbox client program in windows is syncing this freshly downloaded file, I can't do an audio conversion on it because its being uploaded.

I need a better way of testing if a file is free, or I guess to expand on that, I need to know if it is dropbox that is locking the file. All I can do right now is have something pop up saying the file is still in use...do you want to wait for it to free up, or cancel. He clicks the try again button a few times...but due to flaky internet connections it still seems like its the program that isn't work, and that's when I get the phone call saying its still not working.

Here is the whole process :

VB.NET:
    Sub Convert_List()
        For Each Path In ListBox1.Items
            Dim cur_file_info = My.Computer.FileSystem.GetFileInfo(Path)
            Dim new_file_name As String = cur_file_info.Directory.ToString & "\" & Strings.Left(cur_file_info.Name, cur_file_info.Name.Length - cur_file_info.Extension.Length) & ".mp3"

            Dim counter As Integer = 0
            Dim last_check_file_size As Integer = 0

            Do While FileInUse(cur_file_info.FullName) = True
                counter = counter + 1
                If counter > 3000 Then
                    cur_file_info = My.Computer.FileSystem.GetFileInfo(Path)
                    If cur_file_info.Length > last_check_file_size Then

                        last_check_file_size = cur_file_info.Length
                        If frm_Still_DL.Visible = False Then
                            frm_Still_DL.Show()
                            frm_Still_DL.Refresh()
                        Else
                            frm_Still_DL.BringToFront()
                            frm_Still_DL.Refresh()
                        End If

                        counter = 0
                    Else
                        frm_Still_DL.Hide()
                        counter = 0
                        Dim ok As Integer = MsgBox("There was a problem, dropbox may still be using the audio file.  Click ok to keep waiting (recommended) or click cancel to come back and try later!", MsgBoxStyle.OkCancel)
                        If ok <> 1 Then
                            Me.Close()
                        End If
                    End If

                    End If
            Loop
            frm_Still_DL.Hide()
            If My.Computer.FileSystem.FileExists(new_file_name) = False Then
                Shell(Application.StartupPath & "\sox.exe " & """" & Path & """" & " " & """" & new_file_name & """", AppWinStyle.MinimizedNoFocus, True)
            End If

            Do While FileInUse(cur_file_info.FullName) = True
            Loop
 

            My.Computer.FileSystem.DeleteFile(cur_file_info.FullName)
        Next

      
        MsgBox("All Files Converted!")
    End Sub

I know that dropbox offers an API, but i see no reference to visual basic. I also think it is a bit out of the way to do a simple audio conversion.

To beat you to it, I have done some research. There is a program handle.exe, but I don't think I can release it with my software. I have also found many hits regarding a google search "what process is locking my file" but all the solutions are huge, bulky and still seem like I'm going out of my way to do something so simple.

I'm feel VERY defeated by these AU files and dropbox.
Any advice as to what I should do next is very welcome.

Thanks for taking the time to read this.
Nick
 
It comes down to this. While the dropbox client program in windows is syncing this freshly downloaded file, I can't do an audio conversion on it because its being uploaded.
So at this stage dropbox client is reading the file. For your conversion you also only need to read the source file, but you are waiting for write access (FileAccess.ReadWrite). Most applications just reading a file will allow others to read it at the same time. You should test for FileAccess.Read. Only after conversion write access to source file is needed if it is to be deleted.

FileShare flag is about subsequent access, what happens when other party tries to open the file once you have aquired it, it has no relevance to this test and should be left out (which will leave it to default FileShare.Read).
 
Ok, there has been an update.

I moved everything off dropbox, and he still had problems. At this point, I think it may be some wacky antivirus program, or something locking the file for a short time.

Using what you said John, I am not doing any moves or copies. I am taking the au file that is there, and using the converter to make an additional file that is the mp3. This way the source file is only ever read.

Every step of the way, every change I have made works fine on my system... so until he is up I wont know if this works. I know for a fact that asap, early next week I'm getting on his laptop and testing this my self.

Thanks for the reply!
Nick
 
Back
Top