11 Replies - 249 Views - Last Post: 10 February 2012 - 06:56 AM Rate Topic: -----

Topic Sponsor:

#1 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 19-January 12

Copy file via reasources

Posted 08 February 2012 - 03:55 AM

Is there any way of copying a txt file situated in the resources to a directory of the computer from the program itself?
Is This A Good Question/Topic? 0
  • +

Replies To: Copy file via reasources

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Copy file via reasources

Posted 08 February 2012 - 06:45 AM

Yes, you can use the GetManifestResourceStream and read it in and write it back to your path.

A quick search found this result.

Basically all you're doing is getting it from the resource, opening it with a streamreader and then using streamwriter to write it back to your path & filename.

This post has been edited by CharlieMay: 08 February 2012 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 19-January 12

Re: Copy file via reasources

Posted 08 February 2012 - 06:53 AM

View PostCharlieMay, on 08 February 2012 - 06:45 AM, said:

Yes, you can use the GetManifestResourceStream and read it in and write it back to your path.

A quick search found this result.

Basically all you're doing is getting it from the resource, opening it with a streamreader and then using streamwriter to write it back to your path & filename.


Wow this might be a really stupid question but how do i get this to work? The website just gives the coding to a module.But what do i do with it?
I am not trying to make someone do my homework for me but i genuinely don't know what to do with the module.Could you make a sample project and show me.
Again.I am not trying to make you do my homework,i am just clueless right now.
A sample project would be immensely appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Copy file via reasources

Posted 08 February 2012 - 07:09 AM

Then I would place it in an event that will perform this operation (like a button.click) and use that forms name as the GetType parameter.

Just make sure you don't forget the imports at the top of your code.
Was This Post Helpful? 0
  • +
  • -

#5 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 19-January 12

Re: Copy file via reasources

Posted 08 February 2012 - 08:53 AM

View PostCharlieMay, on 08 February 2012 - 07:09 AM, said:

Then I would place it in an event that will perform this operation (like a button.click) and use that forms name as the GetType parameter.

Just make sure you don't forget the imports at the top of your code.


I still get an error in the line
Using input As New StreamReader(source)



It says "Value cannot be null.
Parameter name: stream"

What am i doing wrong?
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Copy file via reasources

Posted 08 February 2012 - 09:16 AM

OK, did you read where it says to make sure you set the Build Action for the file to "Embedded Resource" ? Because that would throw such an error.

Quote

For a more general solution, add the file to your solution, and make sure you set its "Build Action" property to "Embedded Resource". The application below expects to find an embedded resource named "TextFile1.txt" and places it in its working directory:

Was This Post Helpful? 0
  • +
  • -

#7 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 19-January 12

Re: Copy file via reasources

Posted 09 February 2012 - 09:54 AM

View PostCharlieMay, on 08 February 2012 - 09:16 AM, said:

OK, did you read where it says to make sure you set the Build Action for the file to "Embedded Resource" ? Because that would throw such an error.

Quote

For a more general solution, add the file to your solution, and make sure you set its "Build Action" property to "Embedded Resource". The application below expects to find an embedded resource named "TextFile1.txt" and places it in its working directory:

How do i set the Build Action for the file to "Embedded Resource" ?
I am confused? is this under properties?
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Copy file via reasources

Posted 09 February 2012 - 10:55 AM

Click on your textfile that appears in your solution explorer, in the properties is build action.
Was This Post Helpful? 0
  • +
  • -

#9 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 19-January 12

Re: Copy file via reasources

Posted 10 February 2012 - 01:55 AM

View PostCharlieMay, on 09 February 2012 - 10:55 AM, said:

Click on your textfile that appears in your solution explorer, in the properties is build action.


Brilliant! worked like a charm.Thank You
I have one last question, How do i modify the code so that the user can copy the textfile to any directory he wants to instead of the textfile copying itself automatically to the installation directory.
Could this be done with a Savefiledialog? if so what modifications should i add to the coding.
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Copy file via reasources

Posted 10 February 2012 - 05:00 AM

Yes, use a savefiledialog and pass the resulting path to the streamwriter.
Was This Post Helpful? 0
  • +
  • -

#11 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 19-January 12

Re: Copy file via reasources

Posted 10 February 2012 - 05:38 AM

View PostCharlieMay, on 10 February 2012 - 05:00 AM, said:

Yes, use a savefiledialog and pass the resulting path to the streamwriter.

I am a newbie,i get an error when i try it.I haven't learnt any of these.I get an error

        Dim currentAssembly As Assembly = Assembly.GetExecutingAssembly()
        Dim streamName As String = "TextFile1.txt"
        Dim contents As String

        Using source As Stream = currentAssembly.GetManifestResourceStream(GetType(Form1), _
                                                                           streamName)
            Using input As New StreamReader(source)
                contents = input.ReadToEnd()
            End Using
        End Using

        Using output As New StreamWriter("file.txt")
            SaveFileDialog1.ShowDialog()
            output.Write(SaveFileDialog1.FileName)
        End Using

    End Sub



What am i doing wrong?
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Copy file via reasources

Posted 10 February 2012 - 06:56 AM

You need to get the path from the dialog and then set it in the using statement where you inform the StreamWriter of the path and file.

Also, there should be a variable that holds what the dialog returned (String).

Have you researched how to use the SaveFileDialog, there is an example in the MSDN page that is very clear cut.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1