[VB2005] [RESOLVED] Attach file to application
Hey everyone,
so concept is simple, I have been trying to attach a file to my application.
I would like a simple button that when I click on it the file gets saved on the drive.
In order to do this I went in properties of my project, then resources then added the file.
I added my button:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ExportAssembly("my_file_name", "C:")
End Sub
ExportAssembly takes the name of my file, as well as the path.
The function is a per below:
Code:
Public Sub ExportAssembly(ByVal name As String, ByVal folder As String)
Dim myAssembly As System.Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
Dim myStream As Stream = myAssembly.GetManifestResourceStream(myAssembly.GetName.Name + "." + name)
Dim byteExeFile(myStream.Length) As Byte
MessageBox.Show(myStream.Length)
myStream.Read(byteExeFile, 0, myStream.Length)
Dim myTempFile As FileStream = New FileStream(folder + "\" + name, FileMode.Create)
myTempFile.Write(byteExeFile, 0, myStream.Length)
myTempFile.Close()
End Sub
So it's pretty straigh forward, declaring the stream reading the resource file and writing it on the drive...
The problem is that when I run the program at the line "Dim byteExeFile(myStream.Length) As Byte" I get the following error:
"NullReference Exception was unhandelled"
"Object reference not set to an instance of an object."
I have tried many things I also have on top of my code
Imports System
Imports System.IO
Imports System.Text
...
it's been frustrating because I had such a hard time starting and finding about these information to link a file but right now I am stuck...
is there something that I am missing ??
Thanks for any help
Re: Attach file to application
When a null reference exception is thrown you need to test all the references on the offending line to see which one is null. In your case the only possibility on that line is myStream. That would mean that the line above is not creating a Stream, which would imply that the name you're passing is invalid.
Re: Attach file to application
Hi Thanks for the reply
I am fairly sure that I am passing the proper name:
name = "Book1.xls" and the folder is "C:"
At the line where i get the error I also get "Use the "new" keyword to create an instance of an object" ???
I did a monkey test, myAssembly.getName.Name gives "WindowsApplication2" wich is the name of my project. And name is "Book1.xls"
so myStream is WindowsApplication2.Book1.xls
not sure why it would not be defined properly ??
thanks
Re: Attach file to application
Is this a 2005 application? If so then I'd suggest just adding your resources on the Resources tab of the project properties. You can then access them via My.Resources, so all that reflection stuff is handled for you.
Re: Attach file to application
Yes it's 2005
I did read indeed that I could access it through "My.Resources" ... but I have no idea how to do it ?
That's the only way I found when I googled on the subject...
Re: Attach file to application
Add your resources on the Resources tab of the project properties, then get the value from the property of My.Resources of the same name. If you add a file named SomeFile then you get the My.Resources.SomeFile property in code.
Re: Attach file to application
Thanks,
I did type "My.Resources.Book1.xls"
from there I had different options using the code help .. I went to "Copy" but it's about "arrays" ? how can i do if i just want to copy the file just on a path specified through an argument ? (pretty much copying it on the disk)
Also you seem to know quite a lot, how can i learn about all these hidden options ? for example how can i know that i can do "My.resources" ? or that i have sometimes to do "Imports IO" .... ?
I am interested by these things but there is no way where i can find a hierarchal view of things showing everything...
every book start with variable types and things like that but none of all these tricks....
thanks !
Re: Attach file to application
When you add a file resource it will be returned by My.Resources in one of two ways. If the file itself contained plain text, like a TXT, XML or HTM file, then My.Resources will return it as a String object. You can then save that string to a file as you would any other string. If the file is a binary file, like an XLS file, then My.Resources will return it as a Byte array. You can then save that data to a file as you would any other binary data. There are various ways you can implement the details but the easiest way to save text to a file is with IO.File.WriteAllText and the easiest way to save a Byte array is with IO.File.WriteAllBytes.
As for these "tricks" and "hidden options", they aren't that at all. They're just standard operations that you will learn as you go. My advice to absolutely everyone is to ALWAYS consult the MSDN library first whenever you need information. More often than not you will find what you need, particularly as you get more experienced and able to interpret what it says. One of the main advantages of adopting that habit though, is that you will often pick up bits of information that you weren't looking for but are still helpful, either now or in the future. You will also pick up terms and such that give you other avenues to search and find more information. The key to learning is reading, so do as much of it as you can.
Re: Attach file to application
Hey
so following your advices this is what I did:
Code:
If File.Exists(path) = False Then
Dim sw As StreamWriter = File.CreateText(path)
' suppose Book1 is a string file (book1.txt)
sw.WriteLine(My.Resources.Book1.ToString())
sw.Flush()
sw.Close()
End If
So the file gets created, but in the file it writes : "System.Byte[]" ???
Regardind MSDN I do spend quite some time there... but I find it it's more for people who know some stuff....
I have started visual basic from scratch just about 5 days ago....
I just looked for code around and i am building on it to improve and learn, in my mind that's the way I like...
Re: Attach file to application
If you read my previous post again you'll see that I told you exactly how to save a Byte array to a file. A Byte array is NOT text so you don't save it to a text file.
Re: Attach file to application
hi
sorry i thought the code you gave me were part of a bigger code.... I did not know that was all i had to type...
So i did :
IO.File.WriteAllText("C:\file.qry", My.Resources.myquerry.ToString())
but it did not work,
well i did get a file file.qry, but inside i get "File.syste.byte" same as previously... (so actually i was not wrong my previous way)...
I think the My.Resources.myqueer.ToString() returns the wrong thing, how can i do it so that it retunrs the inside of the file ?
Thanks
and when I do my.resources.myquerry.tostring it returns me strings, how about if i wanted myquerry was byes ? how could i extract bytes ?
thanks !
Re: Attach file to application
I have said several times that a Byte array is NOT text so you don't write it to a text file. You need to read a bit more carefully:
Quote:
the easiest way to save a Byte array is with IO.File.WriteAllBytes.
Re: Attach file to application
my file is a text file, i can open it with wordpad and see perfectly the content ... but when i do IO.File.WriteAllText("C:\file.qry", My.Resources.myquerry.ToString()) the content does not reflect the content of the file i added...
I was also asking how it would be done IF it was a byte file... because there is only a "ToString" method...
but right now it does not work with the ToString option...
Re: Attach file to application
You said in an earlier post that your file is an XLS.
Regardless, I have said over and over that if you want to save a Byte array you call WriteAllBytes. If My.Resources.myquery returns a Byte array then you pass that Byte array to IO.File.WriteAllBytes. No calling ToString, no calling WriteAllText.
Re: Attach file to application
vb Code:
IO.File.WriteAllBytes("C:\file.qry", My.Resources.myquerry)
Re: Attach file to application
Oh ok !!!
I got it :
IO.File.WriteAlltext("C:\file.qry", My.Resources.myquerry)
See i did not know that by doing "my.resources.myquerry" it would return me somethign... i thought i had to add a function to it , for example "ToString" ...
What is strange is that myquerry is text (it;s a .qry file it can be opened with wordpad) but it only works when i do io.file.writeallbytes... not alltext... ??
if i do byte it says that my.resource.myquerry is a one dimensional byte array... ??