将音频文件从Windows窗体应用程序资源复制到硬盘驱动器

时间:2014-10-11 04:13:49

标签: vb.net audio visual-studio-2013 resources copy

使用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)

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

3 个答案:

答案 0 :(得分:1)

这是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,让我知道你的去向。

答案 1 :(得分:1)

以下是Code Nice And Easy:

Dim FilePath AS String = Application.StartupPath + "\From_Resource.wav"
IO.File.WriteAllBytes(FilePath,My.Resource.click1)

然后你可以检查它是否存在:

If IO.File.Exists(FilePath) Then MsgBox("File Exists")

还有一个技巧,在默认播放器中播放:

Process.Start(FilePath)

答案 2 :(得分:1)

谢谢大家的建议。这就是我提出的执行我需要的任务的原因。

Dim ms As New MemoryStream
My.Resources.click1.CopyTo(ms)
Dim AudioFile() As Byte = ms.ToArray
File.WriteAllBytes(toPath2 & "\click1.wav", AudioFile) '<-- toPath2 is a specific folder I am saving to
ms.Close()