how do you copy/move a file (image,accdb,...) out of resources?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

57 Replies - 6315 Views - Last Post: 17 May 2012 - 11:40 AM Rate Topic: -----

#1 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 10:25 AM

I am making a VB.NET application linked to a database. when you press the button 'generate' it is writes (fairly simple) webpages to a folder in your destkop. what I tried was adding a build-in uploader to my website, but my host didn't supported that. ok, not that big of a deal, and completely out of context but now let's get to the core of my problem;
I wanted to have an image in the resources (none made yet) and have it copied out the resources and placed along with the webpages (to use it as background image). I have tried a lot (clearly not everything or it would work) but I just don't manage to solve this. the problem is I need to be able to do this, since I want to have my .accdb in the resources to, so that it copies out when used an writes (an updated version) back to the resources and deletes the external file. but I never seem to manage to do this. I tried to read,write and other things it with IO and everything, just getting the message that it cannot be read. but I never seem to find how to copy/move it outside the program. the code I am using now looks like this:
Imports System.Reflection
Imports System.IO 'this is for the writing of the .HTML files
Imports System
Public Class webGenerator

    Private Sub webgenerating_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles webGenerating.DoWork
        Dim naam As String = NaamTextBox.Text.ToString
        Dim achternaam As String = AchternaamTextBox.Text.ToString
        Dim leeftijd As String = LeeftijdTextBox.Text.ToString
        Dim ID As String = IDTextBox.Text.ToString
        Dim destkop As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Directory.CreateDirectory(destkop + "\websites\")
        Dim writer As StreamWriter = New StreamWriter(destkop + "\websites\" + naam + "_Profilepage.html")
        writer.WriteLine("<html>")
        writer.WriteLine("<head><title>Hello world!</title><style>html{background-color:#b0c4de;}#profile{ position: relative; top: 5em; left: 6em; }</style></head>")
        writer.WriteLine("<body>")
        writer.WriteLine("<div id='profile'>")
        writer.WriteLine("<p>your name is: " + naam + " " + achternaam + "</p>")
        writer.WriteLine("<p>your age is: " + leeftijd + "</p>")
        writer.WriteLine("<p>with ID " + ID + "</p>")
        writer.WriteLine("</div>")
        writer.WriteLine("</body>")
        writer.WriteLine("</html>")
        writer.Close()
        Me.PersoneelBindingSource.MoveNext()
    End Sub

    Private Sub PersoneelBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles PersoneelBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.PersoneelBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.BedrijfDataSet)

    End Sub

    Private Sub webGenerator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'BedrijfDataSet.personeel' table. You can move, or remove it, as needed.
        Me.PersoneelTableAdapter.Fill(Me.BedrijfDataSet.personeel)

    End Sub

    Private Sub generateAll_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles generateAll.DoWork
        Dim amount As Integer = Me.PersoneelBindingSource.Count
        Dim current As Integer = 0
        Me.PersoneelBindingSource.MoveFirst()
        While current < amount
            If webgenerating.IsBusy = False Then
                webgenerating.RunWorkerAsync()
                current = current + 1
            End If
        End While
    End Sub

    Private Sub btnGenerateAll_Click(sender As Object, e As EventArgs) Handles btnGenerateAll.Click
        MsgBox("generating all webpages, starting when you close this pop-up box", MsgBoxStyle.Information)
        generateAll.RunWorkerAsync()
        MsgBox("Done!", MsgBoxStyle.Information)
    End Sub

    Private Sub btnGenerateThis_Click(sender As Object, e As EventArgs) Handles btnGenerateThis.Click
        MsgBox("generating a webpage for this profile only: " + Me.NaamTextBox.Text.ToString, MsgBoxStyle.Information)
        webGenerating.RunWorkerAsync()
        MsgBox("Done!", MsgBoxStyle.Information)
    End Sub
End Class



any help is appreciated, if you have suggestions by the way I am intrested to hear them

Is This A Good Question/Topic? 0
  • +

Replies To: how do you copy/move a file (image,accdb,...) out of resources?

#2 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 10:41 AM

well, you could use this code, if you want to copy a file from resources:

 Dim filepath As String = Application.StartupPath & "FileToCopy"
 Dim MyStream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("YourAssemblyName.FileToCopy")
                Dim MyFileStream As New System.IO.FileStream(filepath, FileMode.Create)
                Dim MyBinaryWriter As New BinaryWriter(MyFileStream)
                Try
                    Dim MyByte As Byte = MyStream.ReadByte
                    While Not MyByte = -1
                        MyBinaryWriter.Write(MyByte)
                        MyByte = MyStream.ReadByte
                    End While
                Catch ex As Exception
                Finally
                    MyFileStream.Close()
                End Try
                


you will have to replace FileToCopy with your file name, including the extension, and YourAssemblyName with the application root namespace, which can be found by going to Project - Properties - Application. Also, your resource file must have the built action set to Embedded Resource.
As for the copy/moving/deleting the file after is created, see system.IO namespace.

This post has been edited by rusoaica: 12 May 2012 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 10:56 AM

thanks, but as I run it, it doesnt seem to do anything. (nor does it give an error tough) so would you mind explaining what you do and why, since I'd like to understand what I am using (for eventual improving)
Was This Post Helpful? 0
  • +
  • -

#4 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:02 AM

what is the name of the resource file? did you set its built action to Embedded Resource? show me the code, how you implemented my example.
Was This Post Helpful? 0
  • +
  • -

#5 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:08 AM

sure, the code I used is this:
Private Sub btnResource_Click(sender As Object, e As EventArgs) Handles btnResource.Click
        Dim filepath As String = Application.StartupPath & "bedrijf.accdb"
        Dim MyStream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("eindwerk.bedrijf.accdb")
        Dim MyFileStream As New System.IO.FileStream(filepath, FileMode.Create)
        Dim MyBinaryWriter As New BinaryWriter(MyFileStream)
        Try
            Dim MyByte As Byte = MyStream.ReadByte
            While Not MyByte = -1
                MyBinaryWriter.Write(MyByte)
                MyByte = MyStream.ReadByte
            End While
        Catch ex As Exception
        Finally
            MyFileStream.Close()
        End Try
    End Sub


the resource is called resource.resx and I think I set it to embed... gonna check right away, until further post u can assume it is set correctly. anyway, still haven't you explained it. would you mind to do so?
Was This Post Helpful? 0
  • +
  • -

#6 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:11 AM

system.io.stream = provides a generic view of a sequence of bytes.
Assembly.GetManifestResourceStream = loads the specified manifest resource from this assembly (GetExecutingAssembly gets the assembly that contains the code that is currently executing).
System.IO.FileStream = exposes a stream around a file, supporting both synchronous and asynchronous read and write operations
BinaryWriter = writes primitive types in binary to a stream and supports writing strings in a specific encoding
ReadByte = reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
While Not MyByte = -1
MyBinaryWriter.Write(MyByte)
MyByte = MyStream.ReadByte
End While - this reads and writes bites from your stream, until it returns -1, which means end of stream.
Was This Post Helpful? 0
  • +
  • -

#7 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:14 AM

it may just be me, but I can't find the part specifying the output folder. could that be the problem? (btw resources are embed)
Was This Post Helpful? 0
  • +
  • -

#8 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:19 AM

you must set the built action to Embedded Resource for the bedrijf.accdb file, not for resource.resx. You can do this by clicking your resource file (bedrijf.accdb) in the solution explorer. In the Properties section, you will find Build action, which is by default set to none or compile. This code WILL NOT work if the resource file's built action property is not set to Embedded Resource.

Dim filepath As String = Application.StartupPath & "bedrijf.accdb"


This is the path where the file will be extracted. You can change it to whatever you want, for example:

Dim filepath As String = "C:\bedrijf.accdb"

Was This Post Helpful? 0
  • +
  • -

#9 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:27 AM

I have included a screen capture to show you the configuration. it still doesn't work and in which file is the database supposed to be moved to?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:29 AM

as i can see from your picture, you set the built action to embedded resource for the resource.resx file, not for bedrijf.accdb. Dont click the file in the left of the screen, but the one in the Solution Explorer, in the right of the screen. It is the third, under about.vb and app.config

This post has been edited by rusoaica: 12 May 2012 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#11 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:33 AM

well I can't, if I click the bedrijf.accdb file it gives this:

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#12 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:37 AM

as i said, dont click on the thumbnail file, but on the one in the list on the right of the screen - Soultion explorer. If you look to your first picture, under built action, there is a filed named File Name, which is resources.resx, not bedrijf.accdb. So, you set the built action for the resx file, not the accdb one (you can also see that in the solution explorer list, the file highlighted/selected is resources.resx)
Was This Post Helpful? 0
  • +
  • -

#13 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:49 AM

that is not possible for as far as I can see (geesh I can already see you facepalming)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#14 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 201
  • Joined: 10-March 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 11:55 AM

it wont be a facepalming, because im a very calm person :) i will explain once again, step by step:

in the last image you posted, if you look at the Properties panel (the one with the blue border), right under the :::PROPERTIES::::::::: you can see "resource.resx File Properties" - those are the properties for the resources.resx file, NOT for the accdb. So, click on that drop-down list ("resources.resx File Properties") and chose bedrijf.accdb. After that, set its buit action to embedded resource.
Was This Post Helpful? 0
  • +
  • -

#15 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Re: how do you copy/move a file (image,accdb,...) out of resources?

Posted 12 May 2012 - 12:04 PM

I know what you try to show me, with an image it is perfectly possible. however when you try the same with an acces database it won't change the build action

modification:
A first chance exception of type 'System.OverflowException' occurred in eindwerk.exe
is what you get when you run it...
I REALLY feel bad for troubling you so much :(

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by digitaldevelopment: 12 May 2012 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »