File to app and set to always copy....
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 ExceptionMessageBox.Show(ex.Message)
End Try End If
Well I guess you could base64 text encoded it into your code as a string constant......
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
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...
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.
Dim folder As New IO.DirectoryInfo(Environment.CurrentDirectory)
My.Computer.FileSystem.CopyFile(folder.Parent.Parent.FullName & "\Resources\myFileName", destinationPath)
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