将音频文件从 Windows 窗体应用程序资源复制到硬盘
Posted
技术标签:
【中文标题】将音频文件从 Windows 窗体应用程序资源复制到硬盘【英文标题】:Copy audio file from Windows Form Application Resource to Hard drive 【发布时间】:2014-10-11 04:13:49 【问题描述】:使用 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)
如何将音频资源复制到硬盘?
【问题讨论】:
应该和这个原理一样:***.com/a/16205403/495455 【参考方案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 关于在发布前详细说明您的尝试,让我知道您的进展。
【讨论】:
我尝试了这个解决方案并提出了这个错误:'对象引用未设置为对象的实例。'【参考方案2】:这里的代码又好又简单:
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)
【讨论】:
【参考方案3】:感谢大家的建议。这就是我想出的执行我需要的任务的方法。
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()
【讨论】:
以上是关于将音频文件从 Windows 窗体应用程序资源复制到硬盘的主要内容,如果未能解决你的问题,请参考以下文章