dcsimg
Results 1 to 11 of 11
  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Resolved [RESOLVED] Serialized file in My.Resources

    Hi all, can I deserialize a serialized file in My.Resources ? How ?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Serialized file in My.Resources

    When you add a file to the Resources page of the project properties, a corresponding property is added to My.Resources. The actual type of that property depends on the type of file you added. What type of file did you add and what is the type of that property? How exactly do you want to use the data, e.g. do you want to write the data out to a new file or use it somehow in your app?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Re: Serialized file in My.Resources

    I serialize a text file, then I put in my.resource. I want deserialize it only for reading, no writing.
    I'm able to deserialize a file on disk with this code, but not in my.resource
    Code:
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim Table = DeserializzaTabelleComuni()
            Me.ComboBox1.BeginUpdate()
            Me.ComboBox1.DataSource = Table
            Me.ComboBox1.EndUpdate()
        End Sub
    
        Private Function DeserializzaTabelleComuni() As List(Of Comune)
            Dim bf As New BinaryFormatter
            Dim retValue As List(Of Comune)
            Using fs As New FileStream("F:\Download\listacomuni.dat", FileMode.Open)
                retValue = TryCast(bf.Deserialize(fs), List(Of Comune))
            End Using
            Return retValue
        End Function
    Last edited by patel45; Dec 16th, 2017 at 10:06 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Serialized file in My.Resources

    Quote Originally Posted by jmcilhinney View Post
    What type of file did you add and what is the type of that property?
    You only answered the first part of that question. If you add a text file as a resource then the property should normally be type String but I'm not sure whether that will be the case if the extension is ".dat". Basically, you can pass any type of Stream where you're passing a FileStream in your current code but exactly what type of Stream it will be and how you get it depends on the answer to the question I asked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Re: Serialized file in My.Resources

    I'm sorry but I thought a serialized file was only binary, It is not so ?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Serialized file in My.Resources

    I will take no further part in this discussion until you answer the question I asked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Re: Serialized file in My.Resources

    Quote Originally Posted by jmcilhinney View Post
    What type of file did you add and what is the type of that property?
    I don't know how to answer this question, I serialized a csv file with this code
    Code:
     Private Sub PopolaComuni()
            Dim Table As New List(Of Comune)
            Dim filename As String = "F:\Download\listacomuni.txt"
            Dim fields As String()
            Dim delimiter As String = ";"
            Using parser As New TextFieldParser(filename)
                parser.SetDelimiters(delimiter)
                While Not parser.EndOfData
                    fields = parser.ReadFields()
                    Table.Add(New Comune With {.Istat = fields(0),
                              .Comune = fields(1),
                              .Provincia = fields(2),
                              .Regione = fields(3),
                              .Prefisso = fields(4),
                              .CAP = fields(5),
                              .CodFisco = fields(6),
                              .Abitanti = fields(7), 
                              .Link = fields(8)})
                End While
            End Using
            Dim bf As New BinaryFormatter
            Using fs As New FileStream("F:\Download\listacomuni.dat", FileMode.OpenOrCreate)
                bf.Serialize(fs, Table)
            End Using
    End Sub
    <Serializable>
    Public Class Comune
        Public Property Istat As String
        Public Property Comune As String
        Public Property Provincia As String
        Public Property Regione As String
        Public Property Prefisso As String
        Public Property CAP As String
        Public Property CodFisco As String
        Public Property Abitanti As String 
        Public Property Link As String
    End Class
    Maybe I was not clear in the first post, I want to put in my.resources the binary serialized file, not the source csv file.
    Last edited by patel45; Dec 17th, 2017 at 01:30 AM.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Serialized file in My.Resources

    Quote Originally Posted by patel45 View Post
    I don't know how to answer this question
    You don't know how to find out what type something is? Have you never moused over something in code and had Intellisense pop up information about it, including its type? You don't even need to go that far because Intellisense will even tell you what type something is as you type it (on the keyboard, that is).
    Last edited by jmcilhinney; Dec 17th, 2017 at 02:16 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Re: Serialized file in My.Resources

    Quote Originally Posted by jmcilhinney View Post
    What type of file did you add and what is the type of that property?
    ReadOnly Property My.Resources.Resources.listacomuni as Byte()

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Serialized file in My.Resources

    OK then, so you're getting a Byte array from that property. That means that the way to go is to create a MemoryStream, write your data to that and then pass that where your existing code passes the FileStream, i.e. this:
    vb.net Code:
    1. Using fs As New FileStream("F:\Download\listacomuni.dat", FileMode.Open)
    2.     retValue = TryCast(bf.Deserialize(fs), List(Of Comune))
    3. End Using
    becomes this:
    vb.net Code:
    1. Using ms As New MemoryStream(My.Resources.listacomuni)
    2.     retValue = TryCast(bf.Deserialize(ms), List(Of Comune))
    3. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    120

    Re: Serialized file in My.Resources

    Thank you jmc, all OK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width