2014-10-11 31 views
2

使用Visual Studio 2013拷贝音频文件到硬盘驱动器

我一直在试图从vb.net Windows窗体应用程序拷贝的音频.wav文件无济于事。我已经尝试了一些方法:

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

我试图调用一个子

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) 

如何做一个拷贝的音频资源到硬盘驱动器?

+0

应该是相同的原理是:http://stackoverflow.com/a/16205403/495455 – 2014-10-11 04:16:23

回答

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上发布之前,详细说明您的尝试,让我知道你怎么走。

+0

我尝试这个解决方案以及与此错误想出了:“对象引用不设置到对象的实例“。 – 2014-10-11 20:26:33

1

这里是代码好的,易于:

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) 
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()