VB.NET 2010 - Extracting an application resource to the Desktop

女生的网名这么多〃 提交于 2021-01-29 13:43:56

问题


I am trying to extract an application resource from My.Resources.FILE

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)

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

回答1:


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)

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

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.

EDIT:

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

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

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)

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

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

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

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.



来源:https://stackoverflow.com/questions/63225331/vb-net-2010-extracting-an-application-resource-to-the-desktop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!