dakota367

Hey, I am trying to make a program that will copy a file from itself if the file does not exist in a directory. So, how would I embed the file into the program, and, how do I copy it out Thanks for the help in advance.

Re: Visual Basic General Embedding a file

cybertaz69

File to app and set to always copy....

Code Snippet

If Not My.Computer.FileSystem.FileExists("c:\temp\TextFile1.txt") Then

Try

My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath & "\TextFile1.txt", "C:\temp\TextFile1.txt")

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End If






Re: Visual Basic General Embedding a file

dakota367

Ok, that helps, but what I want is to actually have the program inside the VB file. See, the problem is I want to have the file embedded into the program and be able to extract it to a directory. It is for a sort of installer I am making.




Re: Visual Basic General Embedding a file

cybertaz69

Well I guess you could base64 text encoded it into your code as a string constant......






Re: Visual Basic General Embedding a file

Aw Ali

To embed a file to your program you can use resources. Open the project properties, choose the resources tab and add your file as system resource. The file will be installed along with your application and will be located in Resources folder under application folder. With that, you can slightly modify the code provided by cybertaz69

If Not My.Computer.FileSystem.FileExists(DestinationFilePath) Then

Try

My.Computer.FileSystem.CopyFile(My.Resources.yourFileName, DestinationFilePath)

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End If






Re: Visual Basic General Embedding a file

dakota367

is there any way to make it so that it is in the exe, my program is standalone.




Re: Visual Basic General Embedding a file

Aw Ali

Well, the .exe is a file, not a folder and you cannot place a file in a file. If you follow the above instruction, the file will be intalled with you application and it will placed in special folder "Resources" the same place you will find your exe file. Just test it and it should work for you the way you want.




Re: Visual Basic General Embedding a file

cybertaz69

maybe i'm missing some thing but my.resources stores the file as data not the acuall file.. Example: I add a text file to my.resources and it stores the text that is inside the file into the app.

Do the same thing to an exe and now you have a byte array.

This does take care of the storing it in code but the copyfile command just will not cut it to restore the file.... Copyfile only takes a string as the path for the source....

For an EXE you need to add the byte array to a io.memorystream and then create a io.bianarywriter using the stream. Once created use the bianarywriter write command with the path to write the file.. If you deploy not using the oneclick you'll need to also setup the .net security config...






Re: Visual Basic General Embedding a file

Aw Ali

Resources does store the actual file in Resources directory, but you are right that My.resources return the data not the path name. I have used My.resources to load an image and but did not tested copiying that file with CopyFile.

Still instead of creating the file once again (It was already created and its in Resources folder), an other way of doing it is giving the path name of the file in resources as a parameter to CopyFile. What if I could do this.

Code Snippet

Dim folder As New IO.DirectoryInfo(Environment.CurrentDirectory)

My.Computer.FileSystem.CopyFile(folder.Parent.Parent.FullName & "\Resources\myFileName", destinationPath)






Re: Visual Basic General Embedding a file

Tall Dude

If you want to add a file to your project without it being

an embedded resource, this can be done.

There are pros and cons to this, involving finding

the file in debug vs. published mode.

Embedded resource files that cannot be used directly

by VB, also have their pros and cons.

Read the notes in the code below.

Imports System.io

Public Class Form1

' Section 1 Files added, but not as embedded resources

' Remember, under 'Publish', 'Options', uncheck 'use .deploy file extension'

' ani-bee_cool.gif was added in the IDE with: 'Project'

' 'add existing item', (change the bottom drop down in the dialog

' to 'all files', navigate to the gif location on your hard

' drive, and 'okay' button.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' No 1 works in debug, fails in publish

Try

PictureBox1.Image = Image.FromFile(My.Application.Info.DirectoryPath & "\..\..\ani-bee_cool.gif")

MsgBox("worked with My app info dir path \..\..")

Catch ex As Exception

MsgBox("failed with My app info dir path \..\..")

End Try

Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

' No 2 works in debug, fails in publish

Try

PictureBox1.Image = Image.FromFile(Path.GetDirectoryName(asm.Location) & "\..\..\ani-bee_cool.gif")

MsgBox("worked with asm ..\..")

Catch ex As Exception

MsgBox("failed with asm ..\..")

End Try

' No 3 fails in debug, works in publish

Try

PictureBox1.Image = Image.FromFile(Path.GetDirectoryName(asm.Location) & "\ani-bee_cool.gif")

MsgBox("worked with asm no dots")

Catch ex As Exception

MsgBox("failed with asm no dots")

End Try

' No 4 fails in debug, works in publish

Try

PictureBox1.Image = Image.FromFile(My.Application.Info.DirectoryPath & "\ani-bee_cool.gif")

MsgBox("worked with My app info dir path no dots")

Catch ex As Exception

MsgBox("failed with My app info dir path no dots")

End Try

End Sub

' Section 2, embedded resources. Added from the 'resources' tab of the

' 'My project' double click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' Type no 1 can be used directly

PictureBox1.Image = My.Resources.archer

' Type no 2 need to be massaged through a byte array

' Where is WAV file is an embedded resource

' named jet.wav

Dim b(CInt(My.Resources.jet.Length)) As Byte

My.Resources.jet.Read(b, 0, CInt(My.Resources.jet.Length))

My.Computer.Audio.Play(b, AudioPlayMode.Background)

' Type no 3 needs to be massaged through a byte array,

' and written to disk as a file stream.

' NOTICE !!!!!

' That to get around the 'where is my file ' problem,

' that the file is written AND read using no drive letter

' or pathway. This causes Visual Basic to write the file

' whereever your program is running from and try to

' read it from whereever your program is running from

' Play an MP3 file with a hidden media player

' Example Uses embedded resource file

' named "Paul McCartney - Smile Away.mp3"

Dim b2 As Byte() = My.Resources.Paul_McCartney___Smile_Away

Dim TempFile As System.IO.FileStream = IO.File.Create("songtest.mp3")

TempFile.Write(b2, 0, b2.Length)

TempFile.Close()

AxWindowsMediaPlayer1.Visible = False

AxWindowsMediaPlayer1.URL = "songtest.mp3"

AxWindowsMediaPlayer1.settings.setMode("loop", True)

End Sub

End Class