14 Replies - 1096 Views - Last Post: 16 July 2010 - 01:37 PM Rate Topic: -----

#1 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Data Saving Advice Please

Posted 02 July 2010 - 08:47 AM

Hello All,

I am working on a Windows Forms Application that has a few hundred TextBox objects, a few PictureBox objects, and some graphs. Currently to save the user inputed data I am sending creating a folder to save the images as separate image files, and the inputed data in a txt file. When the user wants to open a previous project the images and TextBox content is then loaded from the "master" folder.

While this is functional, it doesn't seem very professional or efficient. Can someone point me in the direction to save everything in one file, similar to how Excel or Word would do it? A bonus would be to even have a custom file extension, icons, and the ability to double click on the file to have the program load.

It may sound like I am trying to boil the ocean, but any advice is appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Data Saving Advice Please

#2 sired22  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-August 08

Re: Data Saving Advice Please

Posted 02 July 2010 - 09:18 AM

well off the top of my head a really easy method would be just to use the built in zip compressor to just zip the folder and save with a file name like 'project.dat'

you would be amazed how many programs do that.

This post has been edited by sired22: 02 July 2010 - 09:18 AM

Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Data Saving Advice Please

Posted 02 July 2010 - 09:38 AM

Several ideas.

Would you believe that new Word and Excel docs are actually zipped folders? Try changing the extension from ".docx" to ".zip" and opening it. It's actually a directory with nested folders and XML documents. If you wanted to go that route, #ziplib is an easy to implement zip library. You could unzip to %TEMP% or to the Application Data folder when it's open, and then rezip it.

You could also implement some kind of database rather than saving files. You can even store the pictures in the database as BLOBs/Images, if you want, but you could also store them numbered in a directory and just store the reference path in the DB. SQL Compact or Access would be good for this.

I'm sure there are other ways as well. Perhaps use some XML format that you can store each text box and image in. The text is easy, and you could store the images as a base64 string representation of its binary data. Converting an image to binary isn't that hard. This code will convert an image from a file into a base64 string, and back again. Using something like that, you could write the contents of an image file to an XML document, and convert back and forth.
Sub Main()
    Dim image As New Bitmap("c:\dev\testimage.jpg")
    Dim b64string = GetBase64String(image)
    Dim image2 = CreateImage(b64string)
    image2.Save("c:\dev\testimage2.jpg")
End Sub

Function CreateImage(ByVal b64String As String) As Image
    Dim data = Convert.FromBase64String(b64String)
    Dim ms As New MemoryStream(data)
    Return Image.FromStream(ms)
End Function

Function GetBase64String(ByVal image As Image) As String
    Dim stream As New MemoryStream
    image.Save(stream, ImageFormat.Jpeg)
    Dim sr As New BinaryReader(stream)
    stream.Position = 0
    Dim data = sr.ReadBytes(CType(stream.Length, Integer))
    Return Convert.ToBase64String(data)
End Function



EDIT: Changed code to me more relevant.

This post has been edited by insertAlias: 02 July 2010 - 09:57 AM

Was This Post Helpful? 3
  • +
  • -

#4 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 02 July 2010 - 10:27 AM

These are great ideas, thanks a million!
Was This Post Helpful? 0
  • +
  • -

#5 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 05 July 2010 - 10:40 AM

Okay, I have been messing around with the idea of zipping the folder. However I am really not sure of how to get started.

I took a look at the #ziplib link provided by insertAlias, but it seems very complicated.

Are there "built in" compression options with Visual Studio?
Was This Post Helpful? 0
  • +
  • -

#6 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Data Saving Advice Please

Posted 05 July 2010 - 11:11 AM

This may help you... but I'm not sure.
Was This Post Helpful? 0
  • +
  • -

#7 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 05 July 2010 - 11:20 AM

Thanks Jack Eagles1, but the solution seems a little old. When I tried to convert the project to .net 3.5 I ended up with too many errors.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Data Saving Advice Please

Posted 05 July 2010 - 11:32 AM

View PostDarrin, on 05 July 2010 - 04:40 PM, said:

Okay, I have been messing around with the idea of zipping the folder. However I am really not sure of how to get started.

I took a look at the #ziplib link provided by insertAlias, but it seems very complicated.

Are there "built in" compression options with Visual Studio?


There is, but trust me, #ziplib is simpler and more flexible.

Anyway, try the XML idea instead. You can still name the file with your own extension and register that extension to open with your application.
Was This Post Helpful? 0
  • +
  • -

#9 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Data Saving Advice Please

Posted 05 July 2010 - 11:32 AM

Argh... I'll try to find something newer for you.
Was This Post Helpful? 0
  • +
  • -

#10 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 05 July 2010 - 11:53 AM

Honesly I was trying to avoid the XML option out of pure laziness. It looks quite complicated and I have no idea how to even start. I will read up on it a bit and see if I can give it a try.

Thanks
Was This Post Helpful? 0
  • +
  • -

#11 biggerB  Icon User is offline

  • If at first you don’t succeed, call it version 1.0
  • member icon

Reputation: 80
  • View blog
  • Posts: 760
  • Joined: 11-June 10

Re: Data Saving Advice Please

Posted 07 July 2010 - 06:13 AM

View PostinsertAlias, on 02 July 2010 - 08:38 AM, said:

Would you believe that new Word and Excel docs are actually zipped folders? Try changing the extension from ".docx" to ".zip" and opening it. It's actually a directory with nested folders and XML documents.


Wow Who knew Microsoft has been fooling us for so long.. To tell you the truth i did'nt know that this was actually possible..
Great + rep
Was This Post Helpful? 0
  • +
  • -

#12 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 09 July 2010 - 01:54 PM

Okay while the xml route looks like a good option I can't seem to get my head around it, so I am back to the ZIP option.

Just playing around with the GZipStream I came up with the following test/

Imports System.IO
Imports System.IO.Compression
Imports System.IO.Compression.GZipStream


Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sourcefile As FileStream = File.OpenRead("C:\Documents and Settings\Administrator\Desktop\test.png")
        Dim desfile As FileStream = File.Create("C:\Documents and Settings\Administrator\Desktop\test.gz")

        Dim compstream As New GZipStream(desfile, CompressionMode.Compress)

        Dim theByte As Integer = sourcefile.ReadByte()

        While theByte <> -1
            compstream.WriteByte(CType(theByte, Byte))
            theByte = sourcefile.ReadByte()
        End While

        sourcefile.Dispose()
        desfile.Dispose()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sourcefile As FileStream = File.OpenRead("C:\Documents and Settings\Administrator\Desktop\test.gz")
        Dim desfile As FileStream = File.Create("C:\Documents and Settings\Administrator\Desktop\test2.png")

        Dim compstream As New GZipStream(sourcefile, CompressionMode.Decompress)

        Dim theByte As Integer = compstream.ReadByte()

        While theByte <> -1
            desfile.WriteByte(CType(theByte, Byte))
            theByte = compstream.ReadByte()
        End While

        sourcefile.Dispose()
        desfile.Dispose()
    End Sub
End Class



This seems to work, but I can't seem to get this to accept folders instead of only files.

Any ideas?

Thanks
Was This Post Helpful? 0
  • +
  • -

#13 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 09 July 2010 - 02:31 PM

I found another snippet that works for the creation of a ZIP file from a folder. It uses a reference to Shell32.DLL.


Imports System.IO
Imports Shell32
Imports System.IO.Compression
Imports System.IO.Compression.GZipStream


Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim B(21) As Byte
        B(0) = 80 : B(1) = 75 : B(2) = 5 : B(3) = 6
        File.WriteAllBytes("C:\Documents and Settings\Administrator\Desktop\test.zip", B)/>
        Dim SH As New Shell
        Dim SF As Folder = SH.NameSpace("C:\Documents and Settings\Administrator\Desktop\test.zip")
        Dim DF As Folder = SH.NameSpace("C:\Documents and Settings\Administrator\Desktop\test")
        SF.CopyHere(DF)
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Sh As New Shell
        Dim SF As Folder = Sh.NameSpace("C:\Documents and Settings\Administrator\Desktop\test.zip")
        Dim DF As Folder = Sh.NameSpace("C:\Documents and Settings\Administrator\Desktop\Unzipped Files")
        For Each F As FolderItem In SF.Items
            DF.CopyHere(F)
        Next
    End Sub
End Class



The only problem is that I am getting a NullReferenceException on the "DF.CopyHere (F)"???

Also if I have a reference to the Shell32.DLL in my application will that be included in the installation file while ran on another computer?

Thanks
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Data Saving Advice Please

Posted 09 July 2010 - 02:40 PM

GZip won't do folders, as far as I know. That's why we linked #ziplib.

Anyway, what part aren't you understanding about the XML idea? Your XML could look something like this:
<?xml version="1.0" encoding="utf-8"?>
<elements>
  <element name="some element name">
    <strings>
      <string name="some name" value="some value" />
    </strings>
    <images>
      <image name="some image">
        some super long base64 string goes here.
      </image>
    </images>
  </element>
</elements>


You could make it less generic by naming the elements properly, and you could use the methods I provided to create your base64 strings from the images.

If you don't understand that part, basically what you're doing is getting the binary representation of the image, converting it to base64 (to shorten it significantly) and then storing it as text. And from that text you can do the process in reverse to create an image.

Hope that helps. I can post some pointers on creating XML documents with LINQ to XML, if you want.

This post has been edited by insertAlias: 09 July 2010 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

#15 Darrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-June 10

Re: Data Saving Advice Please

Posted 16 July 2010 - 01:37 PM

Okay, I seem to have it figured out. Thanks a bunch insertAlias.

I have now also included a unique extension and registered that extension to open the application.

How can I tell my application that a double click on a file just opened it and to look for the data to enter into each field?

Thanks for the ideas.

This post has been edited by Darrin: 16 July 2010 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1