weblogs.com.pk

Proud To Be Pakistanis!
Welcome to weblogs.com.pk Sign in | Join | Help
in Search

Adeel bin Khalid

"When a Man's Education is Finished; He is finished"

Embedded Resource + Microsoft.VisualBasic.Devices Namespace

I always prefer to develop some lil utilities to make lot of people's life easy, including myself </smile>... For instance I have developed a utility to create backup of sharepoint's sites. And some other utilities like creating insert queries scripts for data in a table, arranging and putting "& _ " in strings of huge SQL queries, and so on..

Sometimes there is a need to add some supportive files to these utilities. There are different techniques to do this but the one is use quite often is that I embed the file in the assembly and at runtime I get it out from the assembly and use it as required. For example in case of creating site backups of sharepoint, a file "stsadm.exe" is required which does all the job. so I include the file in my project and set the Build Action property to Embedded Resource. Then later use the following code to get it out from the assembly.

Dim Strm As IO.Stream
   
Dim Buffer() As Byte
    If
Not My.Computer.FileSystem.FileExists(filename) Then
    Strm = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Root Namespace.filename)
    ReDim Buffer(Strm.Length)
    Strm.Read(Buffer, 0, Buffer.Length)
    Strm.Close()
    My.Computer.FileSystem.WriteAllBytes(filename, Buffer,
False)
    Buffer =
Nothing
    End If

There is a new property introduced in VS2005, Copy to Output Directory. You can set the property according to you need.

And hay has anyone checked the new namespace, Microsoft.VisualBasic.Devices, added in 2.0?? In the code above I have used it couple of time. My.Computer.FileSystem.FileExists and My.Computer.FileSystem.WriteAllBytes. Try it, it got some good stuff to explore.

Published Wednesday, November 01, 2006 5:51 PM by abk

Comments

No Comments
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems