Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. FileToString? StringToFile?

FileToString? StringToFile?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    hai... everyone who know how to convert the File to String? Actually, my idea is doing this to transfer the file to another computer. So, i convert the file (.text file or .exe file) to string and then send to another pc. After this, the pc will received this string then convert the string to File... So, i build up the protocol already but i dont know how to convert the file to string and the convert back from string to file.... anyone can help me.....:wtf: thanks....

    S 1 Reply Last reply
    0
    • L Lost User

      hai... everyone who know how to convert the File to String? Actually, my idea is doing this to transfer the file to another computer. So, i convert the file (.text file or .exe file) to string and then send to another pc. After this, the pc will received this string then convert the string to File... So, i build up the protocol already but i dont know how to convert the file to string and the convert back from string to file.... anyone can help me.....:wtf: thanks....

      S Offline
      S Offline
      Shawn2000
      wrote on last edited by
      #2

      This might be a better way. Ignore the bits about saving it to the database. It converts a file to a Byte Array. I am currently saving pictures to a database in this way. It works great. http://support.microsoft.com/kb/316887[^] Shawn

      C 1 Reply Last reply
      0
      • S Shawn2000

        This might be a better way. Ignore the bits about saving it to the database. It converts a file to a Byte Array. I am currently saving pictures to a database in this way. It works great. http://support.microsoft.com/kb/316887[^] Shawn

        C Offline
        C Offline
        charleslau2855
        wrote on last edited by
        #3

        Thanks for ur reply.... Actually i just know the code from BitMapToString, but i dont know how to use this to convert it from FileToString... So, any suggestion for this function.... Public Function BitmapToString(ByVal sImagePath As String) As String Try Dim data As String Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(sImagePath) ' Save as PNG format (IMHO, much better! // ) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png) data = Convert.ToBase64String(ms.ToArray()) Return data Catch ex As Exception Return String.Empty End Try End Function

        J 1 Reply Last reply
        0
        • C charleslau2855

          Thanks for ur reply.... Actually i just know the code from BitMapToString, but i dont know how to use this to convert it from FileToString... So, any suggestion for this function.... Public Function BitmapToString(ByVal sImagePath As String) As String Try Dim data As String Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(sImagePath) ' Save as PNG format (IMHO, much better! // ) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png) data = Convert.ToBase64String(ms.ToArray()) Return data Catch ex As Exception Return String.Empty End Try End Function

          J Offline
          J Offline
          j45mw
          wrote on last edited by
          #4

          You might want to try some of the code I have in a project I posted on this board about 2 weeks ago. It is using a MySql file but I think the streamreader code might work. http://www.codeproject.com/useritems/WavfilesInDatabases.asp Dim b() As Byte Try b = DBSelectCmd.ExecuteScalar() Dim K As Long K = UBound(b) Dim WriteFs As New FileStream(strOutFile, FileMode.Create, FileAccess.Write) WriteFs.Write(b, 0, K) WriteFs.Close() MessageBox.Show("Wav file has been retrieved and written to application folder") Catch oExcept As Exception MessageBox.Show(oExcept.Message) End Try If this doesn't work, I found some other similar code in MSDN that I think might work but I don't have it on this computer and I will have to wait until Monday to get it from my work computer. Let me know if this either does or doesn't work.

          C 1 Reply Last reply
          0
          • J j45mw

            You might want to try some of the code I have in a project I posted on this board about 2 weeks ago. It is using a MySql file but I think the streamreader code might work. http://www.codeproject.com/useritems/WavfilesInDatabases.asp Dim b() As Byte Try b = DBSelectCmd.ExecuteScalar() Dim K As Long K = UBound(b) Dim WriteFs As New FileStream(strOutFile, FileMode.Create, FileAccess.Write) WriteFs.Write(b, 0, K) WriteFs.Close() MessageBox.Show("Wav file has been retrieved and written to application folder") Catch oExcept As Exception MessageBox.Show(oExcept.Message) End Try If this doesn't work, I found some other similar code in MSDN that I think might work but I don't have it on this computer and I will have to wait until Monday to get it from my work computer. Let me know if this either does or doesn't work.

            C Offline
            C Offline
            charleslau2855
            wrote on last edited by
            #5

            :laugh:hai.. thanks for ur reply... Actually, i do many testing in filestream, but the results are just can convert the file(.txt file) to string. my friend told to me that this is can't to convert the (.exe file) to string. Is true? :confused:

            J 1 Reply Last reply
            0
            • C charleslau2855

              :laugh:hai.. thanks for ur reply... Actually, i do many testing in filestream, but the results are just can convert the file(.txt file) to string. my friend told to me that this is can't to convert the (.exe file) to string. Is true? :confused:

              J Offline
              J Offline
              j1webb
              wrote on last edited by
              #6

              See if this code might work. I don't know if you can "rebuild" data from a table back into a .exe, but I don't know of any reason why you shouldn't. If you FTP a file it is basically being "read" in binary, sent as a binary stream of data and then basically rebuild on the receiving end. Anyway check this out. Do While fdRead.Read() 'strTmp = fdRead.GetString(0) strProgramFile = fdRead.GetString(0) strOtherData = fdRead.GetString(1) strOtherData2 = fdRead.GetString(2) ' Create a file to hold the output. strFileOut = "progrname.exe" 'Me.TextBox2.Text & ".wav" fs = New FileStream(strFileOut, FileMode.OpenOrCreate, FileAccess.Write) bw = New BinaryWriter(fs) startIndex = 0 ' Read bytes into outbyte() and retain the number of bytes returned. retval = fdRead.GetBytes(1, startIndex, outbyte, 0, bufferSize) ' Continue reading and writing while there are bytes beyond the size of the buffer. Do While retval = bufferSize bw.Write(outbyte) bw.Flush() ' Reposition the start index to the end of the last buffer and fill the buffer. startIndex += bufferSize retval = fdRead.GetBytes(1, startIndex, outbyte, 0, bufferSize) Loop ' Write the remaining buffer. Try bw.Write(outbyte, 0, retval - 1) Catch End Try bw.Flush() ' Close the output file. bw.Close() fs.Close() Loop fdRead.Close() Let me know if this works or not.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups