VB.NET 2010-将应用程序资源提取到桌面 [英] VB.NET 2010 - Extracting an application resource to the Desktop

查看:64
本文介绍了VB.NET 2010-将应用程序资源提取到桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 My.Resources.FILE

我发现了如何使用DLL&EXE文件,但我仍然需要有关提取PNG&的代码的帮助.ICO文件.

I have discovered how to do this with DLL & EXE files, but I still need help with the code for extracting PNG & ICO files.

其他文件类型也是如此.(如果可能的话)

Other file types also. (If possible)

这是我当前与DLL&EXE文件.

Here is my current code that works with DLL & EXE files.

Dim File01 As System.IO.FileStream = New System.IO.FileStream("C:\Users\" + Environment.UserName + "\Desktop\" + "SAMPLE.EXE", IO.FileMode.Create)
            File01.Write(My.Resources.SAMPLE, 0, My.Resources.SAMPLE.Length)
            File01.Close()

推荐答案

首先,您拥有的代码不好.使用 My.Resources 时,每次使用属性时,都将提取数据的新副本.这意味着您的第二行使数据写入两次,而第二次只是获取其长度.至少,您应该只获取一次数据并将其分配给一个变量,然后使用该变量两次.您还应该使用 Using 语句创建和销毁 FileStream .更好的是,只需调用 File.WriteAllBytes ,这意味着您不必创建自己的 FileStream 或知道要写入的数据长度.您也不应以这种方式构造文件路径.

First things first, the code you have is bad. When using My.Resources, every time you use a property, you extract a new copy of the data. That means that your second line is getting the data to write twice, with the second time being only to get its length. At the very least, you should be getting the data only once and assigning it to a variable, then using that variable twice. You should also be using a Using statement to create and destroy the FileStream. Even better though, just call File.WriteAllBytes, which means that you don't have to create your own FileStream or know the length of the data to write. You should also not be constructing the file path that way.

Dim filePath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "SAMPLE.EXE")

File.WriteAllBytes(filePath, My.Resources.SAMPLE)

对于您的问题,这里要理解的重要一点是它确实与资源无关.问题实际上是如何保存任何特定类型的数据,这是您可以自己查找的内容.当从 My.Resources 获取属性的值时,获取的数据类型将取决于首先嵌入的文件的类型.如果是二进制文件,例如使用DLL或EXE,您将返回一个 Byte 数组,因此您将数据保存到文件中的方式与其他任何 Byte 数组相同.如果是图片文件,例如PNG,您将返回一个 Image 对象,因此可以像保存其他任何 Image 对象一样保存该对象,例如

As for your question, the important thing to understand here is that it really has nothing to do with resources. The question is really how to save data of any particular type and that is something that you can look up for yourself. When you get the value of a property from My.Resources, the type of the data you get will depend on the type of the file you embedded in first place. In the case of a binary file, e.g. DLL or EXE, you will get back a Byte array and so you save that data to a file in the same way as you would any other Byte array. In the case of an image file, e.g. PNG, you will get back an Image object, so you save that like you would any other Image object, e.g.

Dim filePath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "PICTURE.PNG")

Using picture = My.Resources.PICTURE
    picture.Save(filePath, picture.RawFormat)
End Using

对于ICO文件,您将返回一个 Icon 对象.我将留给您研究如何将 Icon 对象保存到文件中.

For an ICO file you will get back an Icon object. I'll leave it to you to research how to save an Icon object to a file.

确定要解决的实际问题很重要.您显然可以从 My.Resources 获取对象,所以这不是问题.您需要确定该对象是什么类型,并确定如何保存该类型的对象.无论对象来自何处,如何做到这一点都是相同的,因此资源部分无关紧要.考虑一下您必须要做的事情,并编写一个方法来执行该操作,然后调用该方法.

It's important to identify what the actual problem is that you're trying to solve. You can obviously get an object from My.Resources so that is not the problem. You need to determine what type that object is and determine how to save an object of that type. How to do that will be the same no matter where that object comes from, so the resources part is irrelevant. Think about what it is that you have to do and write a method to do it, then call that method.

在您的原始情况下,您可以这样开始:

In your original case, you could start like this:

Dim data = My.Resources.SAMPLE

一旦您编写了该代码(即使您编写了该代码),Intellisense也会告诉您数据是一个 Byte 数组.现在您的实际问题是如何将 Byte 数组保存到文件中,因此编写一个执行该操作的方法:

Once you have written that - even as you write it - Intellisense will tell you that the data is a Byte array. Your actual problem is now how to save a Byte array to a file, so write a method that does that:

Private Sub SaveToFile(data As Byte(), filePath As String)
    '...
End Sub

现在可以开始执行的操作:根据当前情况编写代码以调用该方法,或者编写该方法的实现.有多种将二进制数据(即 Byte 数组)保存到文件的特定方法,但是,正如我所说的,最简单的方法是 File.WriteAllBytes :

You can now which you want to do first: write code to call that method as appropriate for your current scenario or write the implementation of the method. There are various specific ways to save binary data, i.e. a Byte array, to a file but, as I said, the simplest is File.WriteAllBytes:

Private Sub SaveToFile(data As Byte(), filePath As String)
    File.WriteAllBytes(filePath, data)
End Sub

关于调用该方法,您需要记录已经拥有的数据和文件路径:

As for calling the method, you need to data, which you already have, and the file path:

Dim data = My.Resources.SAMPLE
Dim folderPath = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim fileName = "SAMPLE.EXE"
Dim filePath = Path.Combine(folderPath, fileName)

SaveToFile(data, filePath)

足够简单.您需要对其他任何资源执行相同的步骤.如果嵌入了PNG文件,则将发现数据是 Image 对象,或更具体地说,是 Bitmap .然后,您的任务是学习如何将这样的对象保存到文件中.不久之后,您就会发现 Image 类具有自己的 Save 方法,因此可以在您的方法中使用它:

Simple enough. You need to follow the same steps for any other resource. If you embedded a PNG file then you would find that the data is an Image object or, more specifically, a Bitmap. Your task is then to learn how to save such an object to a file. It shouldn't take you long to find out that the Image class has its own Save method, so you would use that in your method:

Private Sub SaveToFile(data As Image, filePath As String)
    data.Save(filePath, data.RawFormat)
End Sub

调用该方法的代码基本上与以前一样,只是在处理完该代码后需要处理 image 对象:

The code to call the method is basically as before, with the exception that an image object needs to be disposed when you're done with it:

Dim data = My.Resources.PICTURE
Dim folderPath = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim fileName = "SAMPLE.EXE"
Dim filePath = Path.Combine(folderPath, fileName)

SaveToFile(data, filePath)
data.Dispose()

在像这样的狭窄范围内创建和处理对象的正确方法是使用 Using 块:

The proper way to create and dispose an object in a narrow scope like this is with a Using block:

Dim folderPath = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim fileName = "SAMPLE.EXE"
Dim filePath = Path.Combine(folderPath, fileName)

Using data = My.Resources.PICTURE
    SaveToFile(data, filePath)
End Using

现在由您决定对ICO文件执行相同的步骤.如果您是动手学习者,那么请动手.

Now it is up to you to carry out the same steps for an ICO file. If you are a hands on learner then get your hands on.

这篇关于VB.NET 2010-将应用程序资源提取到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆