文件不断被我的代码使用,无法手动或使用代码删除

发布于 2025-01-06 16:39:16 字数 4147 浏览 1 评论 0原文

我正在尝试使用谷歌的语音 API 进行语音识别。

我修改了 UPLOADFILEEX 函数,可以在 codeproject 上找到...

我希望删除的文件是 C:\record.flac

这是下面的函数

         Public Shared Function UploadFile(ByVal uploadfilename As String, ByVal url As String, ByVal fileFormName As String, ByVal contenttype As String, ByVal querystring As System.Collections.Specialized.NameValueCollection, ByVal cookies As CookieContainer) As String
    If (fileFormName Is Nothing) OrElse (fileFormName.Length = 0) Then
        fileFormName = "file"
    End If
    If (contenttype Is Nothing) OrElse (contenttype.Length = 0) Then
        contenttype = "application/octet-stream"
    End If
    Dim postdata As String
    postdata = "?"
    If Not (querystring Is Nothing) Then
        For Each key As String In querystring.Keys
            postdata += key + "=" + querystring.Get(key) + "&"
        Next
    End If
    Dim uri As Uri = New Uri(url + postdata)
    Dim boundary As String = "----------" + DateTime.Now.Ticks.ToString("x")
    Dim webrequest As HttpWebRequest = CType(Net.WebRequest.Create(uri), HttpWebRequest)
    webrequest.CookieContainer = cookies
    webrequest.ContentType = "audio/x-flac; rate=16000"
    webrequest.Method = "POST"
    Dim sb As StringBuilder = New StringBuilder
    sb.Append("--")
    sb.Append(boundary)
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    sb.Append("Content-Disposition: form-data; name=""")
    sb.Append(fileFormName)
    sb.Append("""; filename=""")
    sb.Append(IO.Path.GetFileName(uploadfilename))
    sb.Append("""")
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    sb.Append("Content-Type: ")
    sb.Append(contenttype)
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    Dim postHeader As String = sb.ToString
    Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader)
    Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "--" + boundary + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    Dim fileStreama As FileStream = New FileStream(uploadfilename, FileMode.Open, FileAccess.Read)
    Dim length As Long = postHeaderBytes.Length + fileStreama.Length + boundaryBytes.Length
    webrequest.ContentLength = length
    Dim requestStream As Stream = webrequest.GetRequestStream
    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
    Dim sendBuffer(Math.Min(4096, fileStreama.Length)) As Byte
    Dim bytesRead As Integer = 0
    Do
        bytesRead = fileStreama.Read(sendBuffer, 0, sendBuffer.Length)
        If bytesRead = 0 Then Exit Do
        requestStream.Write(sendBuffer, 0, bytesRead)
    Loop
    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
    Dim responce As WebResponse = webrequest.GetResponse
    Dim s As Stream = responce.GetResponseStream
    Dim sr As StreamReader = New StreamReader(s)
    Return sr.ReadToEnd
    sr.Dispose()
    s.Dispose()
    fileStreama.Dispose()
    requestStream.Dispose()
    webrequest.Abort()
    responce.Close()

End Function

该函数有效(感谢上帝),但每当我想清除(即删除音频文件)位于 c:) 中,它只是挂起,没有任何反应...

下面是我在 formclose 事件上执行的代码...

      Private Sub Form1_Close(sender As System.Object, e As System.EventArgs) Handles MyBase.FormClosing
    Dim di As New IO.DirectoryInfo("c:\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo
    For Each dra In diar1
        If dra.Name.Contains("record.") Then
            dra.Delete()
        End If
    Next
End Sub

正如您可能可以从我尝试删除所有流的函数中看到的那样和关闭它们,这样文件就不会被访问

,但它仍然挂起,当我尝试手动删除它时......它告诉我它正在被另一个进程(这是我的程序)使用 - (我使用 ffmpeg 将 .wav 转换为.flac 文件,但这不会导致任何问题)...

我做错了什么...

我是否错过了某些流或其他内容的关闭。

顺便说一句,uploadfilename 字符串是 c:\record.flac (仅供您参考 - 我认为这不会有帮助)

I am trying to make a voice recognition thing with google's voice api.

I modified UPLOADFILEEX function that can be found on codeproject...

The file I wish to delete is C:\record.flac

Here is the function below

         Public Shared Function UploadFile(ByVal uploadfilename As String, ByVal url As String, ByVal fileFormName As String, ByVal contenttype As String, ByVal querystring As System.Collections.Specialized.NameValueCollection, ByVal cookies As CookieContainer) As String
    If (fileFormName Is Nothing) OrElse (fileFormName.Length = 0) Then
        fileFormName = "file"
    End If
    If (contenttype Is Nothing) OrElse (contenttype.Length = 0) Then
        contenttype = "application/octet-stream"
    End If
    Dim postdata As String
    postdata = "?"
    If Not (querystring Is Nothing) Then
        For Each key As String In querystring.Keys
            postdata += key + "=" + querystring.Get(key) + "&"
        Next
    End If
    Dim uri As Uri = New Uri(url + postdata)
    Dim boundary As String = "----------" + DateTime.Now.Ticks.ToString("x")
    Dim webrequest As HttpWebRequest = CType(Net.WebRequest.Create(uri), HttpWebRequest)
    webrequest.CookieContainer = cookies
    webrequest.ContentType = "audio/x-flac; rate=16000"
    webrequest.Method = "POST"
    Dim sb As StringBuilder = New StringBuilder
    sb.Append("--")
    sb.Append(boundary)
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    sb.Append("Content-Disposition: form-data; name=""")
    sb.Append(fileFormName)
    sb.Append("""; filename=""")
    sb.Append(IO.Path.GetFileName(uploadfilename))
    sb.Append("""")
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    sb.Append("Content-Type: ")
    sb.Append(contenttype)
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    Dim postHeader As String = sb.ToString
    Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader)
    Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "--" + boundary + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
    Dim fileStreama As FileStream = New FileStream(uploadfilename, FileMode.Open, FileAccess.Read)
    Dim length As Long = postHeaderBytes.Length + fileStreama.Length + boundaryBytes.Length
    webrequest.ContentLength = length
    Dim requestStream As Stream = webrequest.GetRequestStream
    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
    Dim sendBuffer(Math.Min(4096, fileStreama.Length)) As Byte
    Dim bytesRead As Integer = 0
    Do
        bytesRead = fileStreama.Read(sendBuffer, 0, sendBuffer.Length)
        If bytesRead = 0 Then Exit Do
        requestStream.Write(sendBuffer, 0, bytesRead)
    Loop
    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
    Dim responce As WebResponse = webrequest.GetResponse
    Dim s As Stream = responce.GetResponseStream
    Dim sr As StreamReader = New StreamReader(s)
    Return sr.ReadToEnd
    sr.Dispose()
    s.Dispose()
    fileStreama.Dispose()
    requestStream.Dispose()
    webrequest.Abort()
    responce.Close()

End Function

The function WORKS (Thankgod) but whenever I want to clear up (i.e. delete the audio file that is located in the c:) it just hangs and nothing happens...

Below is my code that executes on the formclosing event....

      Private Sub Form1_Close(sender As System.Object, e As System.EventArgs) Handles MyBase.FormClosing
    Dim di As New IO.DirectoryInfo("c:\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo
    For Each dra In diar1
        If dra.Name.Contains("record.") Then
            dra.Delete()
        End If
    Next
End Sub

As you probably may be able to see from the function I HAVE TRIED TO REMOVE ALL THE STREAMS AND CLOSE THEM SO THE FILE IS NOT BEING ACCESSED

BUT it still hangs and when I try manually delete it... it tells me it is being used by another process (which is my program) - (I use ffmpeg to convert the .wav to .flac file but that does not cause any problems)....

What am I doing wrong...

Have I missed the closing of some stream or something.

By the way the uploadfilename string is c:\record.flac (just for your information - I dont think it will help)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2025-01-13 16:39:16

您的函数在处理文件流之前调用 Return 。这意味着您的 Dispose 调用永远不会被触发......并且流保留文件引用。

Your function is calling Return BEFORE disposing of the file stream. This means that your Dispose call is never fired.. and the stream holds onto a file reference.

找回味觉 2025-01-13 16:39:16

我同意 Boo 的观点,即处置前的返回可能是原因。因此,解决方案是将 sr.ReadToEnd 的结果存储在变量中,关闭流,然后返回 var。

但更好的解决方案是在每个必须调用 .Dispose() 的类(也称为实现I一次性)。

这提供了更清晰的代码,因为您可以轻松查看代码中的哪些位置正在使用哪些资源。此外,错误代码也较少,因为 .NET 确保一旦离开 using 块的范围,所有内容都会被处理掉。

I Agree with Boo that the return before the dispose probably is the cause. So the solution would be to store the result of sr.ReadToEnd in a variable, close the streams and then return the var.

But a better solution would be to use the Using statement (http://msdn.microsoft.com/en-us/library/htd05whh.aspx) on every class that you have to call .Dispose() on (aka classes that implement IDisposable).

This gives clearer code because you can easily see what resources are in use where in your code. Also less buggy code as .NET ensures that everything is disposed once you leave the scope of the using block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文