使用Visual Studio 2013

我一直试图从vb.net Windows窗体应用程序复制音频.wav文件无济于事。我尝试了几种方法:

File.Copy(My.Resource.click1, "c:\destination folder", True)

我试过打电话给Sub
Dim ms As New MemoryStream
My.Resources.click1.CopyTo(ms)
Dim ByteArray() As Byte = ms.ToArray
sfr(toPath2 & "\click1.wav", ByteArray)

Public Sub sfr(ByVal FilePath As Byte, ByVal File As Object)
    Dim FByte() As Byte = File
    My.Computer.FileSystem.WriteAllBytes(FilePath, FByte, True)
End Sub

我也尝试过
File.WriteAllText(toPath2 & "\click1.wav", My.Resources.click1)

如何将音频资源复制到硬盘驱动器?

最佳答案

这是tested C# version的VB.Net版本:

Dim asm As Assembly = Assembly.GetExecutingAssembly()
Dim file As String = String.Format("{0}.click1.wav", asm.GetName().Name)
Dim fileStream As Stream = asm.GetManifestResourceStream(file)
SaveStreamToFile("c:\Temp\click1.wav", fileStream)  '<--here is the call to save to disk


Public Sub SaveStreamToFile(fileFullPath As String, stream As Stream)
    If stream.Length = 0 Then
        Return
    End If

    ' Create a FileStream object to write a stream to a file
    Using fileStream As FileStream = System.IO.File.Create(fileFullPath, CInt(stream.Length))
        ' Fill the bytes[] array with the stream data
        Dim bytesInStream As Byte() = New Byte(stream.Length - 1) {}
        stream.Read(bytesInStream, 0, CInt(bytesInStream.Length))

        ' Use FileStream object to write to the specified file
        fileStream.Write(bytesInStream, 0, bytesInStream.Length)
    End Using
End Sub

+1在发布之前详细说明您的尝试,请告诉我您的情况。

关于vb.net - 将音频文件从Windows窗体应用程序资源复制到硬盘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26310984/

10-12 00:37
查看更多