0

I am currently working on a console application to play a freshly created WAV RIFF file, and then delete it. Like I said, it is freshly created, so I need to make sure the file isn't being edited before I start playing it or it will be corrupted. After it plays, I delete it.

Currently, my code looks like this (using System.IO):

Sub Main()

    Dim fileName As String
    fileName = "C:\temp\Burst\Burst.wav"

    While CheckFile(fileName)
    End While

    Try
        My.Computer.Audio.Play(fileName, AudioPlayMode.WaitToComplete)
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

    My.Computer.FileSystem.DeleteFile(fileName)

End Sub

Private Function CheckFile(ByVal filename As String) As Boolean
    Try
        System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
        FileClose(1)
        Return False
    Catch ex As Exception
        Return True
    End Try
End Function

The function I am using to check if the file is opened was created by sealz. I found it here. Unfortunately, however, this function is causing an exception in that after it runs, the program cannot access the file because it is being used by another process. If I remove this function, the file can be opened, played and deleted.

The exception reads as follows:

An unhandled exception of type'System.IO.IOException' occurred in mscorlib.dll Additionalinformation: The process cannot access the file 'C:\temp\Burst\burst.wav' because it is being used by another process.

So the function that is supposed to help determine if the file is being used, is actually causing the file to be opened. It seems like it isn't closing. Is there anyway I can modify this current function to work properly for my application or are there any other ideas on how to tackle this. Thanks for your time.

-Josh

Community
  • 1
  • 1
Josh
  • 7
  • 4

2 Answers2

0

Here is your problem:

System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
FileClose(1)
Return False

A Using will help:

Using _fs as System.Io.FileStream = System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
End Using
Return False

File.Open Returns a Filestream, not an Integer needed for FileClose

David Sdot
  • 2,343
  • 1
  • 20
  • 26
  • David. Thank you for taking time to answer to answer my question. This is working for me perfectly. If I have the file open, the program just waits, but if I close the file, it now plays it and deletes it as it should. Excellent! **Unfortunately, I cannon upvote this answer, because I am only a level 1, but it is working perfectly for me. Thanks again! – Josh Feb 05 '15 at 16:31
0

As far as I get you are trying to check if file exists before playback using System.IO.File.Open however you may do it with File.Exists.

Method File.Exists from System.IO returns true if file exists on path and returns false the otherwise.

Also you are doing it wrong here,

While CheckFile(fileName)
End While

If file is found it will enter into an infinite loop without doing anything other than calling CheckFile repeatedly. If file is not found, it will get out of loop and attempt Audio.Play and FileSystem.DeleteFile and you end up getting a file not found exception.

Here is your code modified and working.

Imports System.IO

Module Module1

    Sub Main()
        Dim fileName As String
        fileName = "C:\temp\Burst\Burst.wav"

        While CheckFile(fileName)
            Try
                My.Computer.Audio.Play(fileName, AudioPlayMode.WaitToComplete)
                'Delete statement here if you want file to be deleted after playback
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End While


        My.Computer.FileSystem.DeleteFile(fileName)
        Console.ReadLine()

    End Sub


    Private Function CheckFile(ByVal filename As String) As Boolean

        If (File.Exists(filename)) Then
            Return True
        Else
            Return False
        End If

    End Function
End Module
Abdullah Leghari
  • 2,332
  • 3
  • 24
  • 40
  • Adbullah. Thanks for taking time to answer my post. You make a great point about adding some code to make sure the file exists before checking to see if it is open. I will add this into my code. However, this does not solve the issue I am having since checking if the file exists doesn't check if it is open. Again, thanks for your response and taking the time to help out! – Josh Feb 05 '15 at 16:36