How can I extract a torrent file from my resources ? (Resolved)
Hi again, on my other VB project, I want to extract a torrent file from my resources, in a special directory without a savedialog and without button again :ehh:. And my research on internet give me only old projects without explications. I do not understand how to do it either :confused: and I think that it's more easy to understand with help of people on this forum :rolleyes:. Srry again for my bad english :D.
This is is an example of what i made :
Code:
Dim filePath = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "1.torrent")
Code:
My.Resources.1 (Idk after...)
Re: How can I extract a torrent file from my resources ?
Firstly, SaveFileDialogs and Buttons are irrelevant.
If you want to save a file then you need a path to save it to. The SaveFileDialog simply provides a way for the user to provide that path but the path is just a String in the end. When you save the file you provide the path as a String. Whether that String is hard-coded or comes from the FileName property of a SaveFileDialog is irrelevant to the action of saving the file.
If you want to save a file - or do anything at all - then you need to start from an event handler. Windows Forms is event-driven, meaning that all actions start by an object raising an event and invoking an event-handler method and you putting some code to execute in that method. Whether the event is the Load of a Form, the Click of a Button, the Tick of a Timer or something else is irrelevant to the code being executed.
So, it appears that you have added this file to your resources on the Resources page of the project properties. That's good. You now access the data from the file (the file itself no longer exists as far as the app is concerned; a resource is data, not a file) via a property of My.Resources. Which data type that property is depends on the type of file you started with. If you started with a text file then the property will be type String. For most file types, the data type will be either Byte(), i.e. Byte array, or UnmanagedMemoryStream. The way you save that data to a file depends on that data type. You can find out what that data type is easily with the help of Intellisense. You can type this code:
vb.net Code:
Dim x = My.Resources.SomeResource
and then mouse over the 'x' variable and Intellisense will tell you what data type is. It will even give you the type of the property before you finish typing that code.
Here is how you can save text, a Byte array or an UnmanagedMemoryStream to a file:
vb.net Code:
File.WriteAllText(filePath, My.Resources.TextResource)
File.WriteAllBytes(filePath, My.Resources.ByteArrayResource)
Using inputStream = My.Resources.StreamResource,
outputStream = New FileStream(filePath, FileMode.Create)
inputStream.CopyTo(outputStream)
End Using
Note that that code to save the file is, again, independent of the fact that you're using resources. File.WriteAllText is the way you save ANY String to a file, File.WriteAllBytes is how you save ANY Byte array to a file and Stream.CopyTo to a FileStream is how you save ANY Stream to a file. This is why you need to break your problem down. If you look for information on how to save resources to files then you're severely limiting the information you can use and you'll miss plenty of relevant information. Saving a resource to a file is two steps: getting data out of the resource and saving data to a file. ANY information on saving data to a file is relevant because it is independent of the source of the data. ALWAYS divide and conquer, i.e. break your problem up into parts, attack each part separately and then combine your partial solutions.
Re: How can I extract a torrent file from my resources ?
Thank you very much, It work now :D !
:thumb:
Re: How can I extract a torrent file from my resources ?
Thank you very much, It work now :D !
:thumb: